在android

时间:2015-10-05 14:02:14

标签: java android

我已经获得了互联网许可 当我尝试从url下载doc文件时得到 java.io.EOFException 在我的情况下,当我在浏览器doc中点击url成功下载到PC但在我的设备上无效。

class DownloadFileFromURL extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread
         * Show Progress Bar Dialog
         */

        @SuppressWarnings("deprecation")
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            try {
                pDialog = new ProgressDialog(getApplicationContext());
                pDialog.setMessage("Downloading file. Please wait...");
                pDialog.setCancelable(false);
                pDialog.show();

            } catch (Exception anfe) {

            }
        }

        /**
         * Downloading file in background thread
         */
        @Override
        protected String doInBackground(String... f_url) {
            int count;
            String downloadedPath = null;
            try {


                String urlStr = f_url[0];
                String fileName = urlStr.substring(urlStr.lastIndexOf('/') + 1);
                URL url1 = new URL(urlStr);
                URI uri = new URI(url1.getProtocol(), url1.getUserInfo(), url1.getHost(), url1.getPort(), url1.getPath(), url1.getQuery(), url1.getRef());
                url1 = uri.toURL();
                String encodeUrl = url1.toString();

                final String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(urlStr);

                File file = new File(Environment.getExternalStorageDirectory() + "/file/downloaded");
                if (!file.exists()) {
                    file.mkdirs();
                }

                downloadedPath = file.getAbsolutePath() + "/" + md5String + "." + extension;
                URL url = new URL(encodeUrl);
                URLConnection conection = url.openConnection();
                conection.setRequestProperty( "Accept-Encoding", "" );
                conection.connect();
                // getting file length
                int lenghtOfFile = conection.getContentLength();

                // input stream to read file - with 8k buffer
                InputStream input = new BufferedInputStream(url.openStream(), 20000);

                // Output stream to write file
                OutputStream output = new FileOutputStream(downloadedPath);

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;
                    // publishing the progress....
                    // After this onProgressUpdate will be called
                    publishProgress("" + (int) ((total * 100) / lenghtOfFile));

                    // writing data to file
                    output.write(data, 0, count);
                }

                // flushing output
                output.flush();

                // closing streams
                output.close();
                input.close();

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return downloadedPath;
        }

        /**
         * Updating progress bar
         */
        protected void onProgressUpdate(String... progress) {
            // setting progress percentage
            //            pDialog.setProgress(Integer.parseInt(progress[0]));
        }

        /**
         * After completing background task
         * Dismiss the progress dialog
         * *
         */
        @Override
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after the file was downloaded
            try {
                pDialog.dismiss();

                // Displaying downloaded image into image view
                // Reading image path from sdcard
                if (file_url!=null){
                    openDocument(file_url);
                }

            } catch (Exception anfe) {

            }

        }

    }
  

10-05 19:30:16.576:W / System.err(9074):java.io.EOFException 10-05   19:30:16.576:W / System.err(9074):at   java.util.zip.GZIPInputStream.readFully(GZIPInputStream.java:202)   10-05 19:30:16.576:W / System.err(9074):at   java.util.zip.GZIPInputStream。(GZIPInputStream.java:98)10-05   19:30:16.576:W / System.err(9074):at   java.util.zip.GZIPInputStream。(GZIPInputStream.java:81)10-05   19:30:16.576:W / System.err(9074):at   com.android.okhttp.internal.http.HttpEngine.initContentStream(HttpEngine.java:468)   10-05 19:30:16.576:W / System.err(9074):at   com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:666)   10-05 19:30:16.576:W / System.err(9074):at   com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347)   10-05 19:30:16.576:W / System.err(9074):at   com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)   10-05 19:30:16.576:W / System.err(9074):at   com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)   10-05 19:30:16.576:W / System.err(9074):at   java.net.URL.openStream(URL.java:470)10-05 19:30:16.576:   W / System.err(9074):at   demo.openfile.MainActivity $ DownloadFileFromURL.doInBackground(MainActivity.java:129)   10-05 19:30:16.576:W / System.err(9074):at   demo.openfile.MainActivity $ DownloadFileFromURL.doInBackground(MainActivity.java:1)   10-05 19:30:16.576:W / System.err(9074):at   android.os.AsyncTask $ 2.call(AsyncTask.java:288)10-05 19:30:16.576:   W / System.err(9074):at   java.util.concurrent.FutureTask.run(FutureTask.java:237)10-05   19:30:16.576:W / System.err(9074):at   android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)10-05   19:30:16.576:W / System.err(9074):at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)   10-05 19:30:16.576:W / System.err(9074):at   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)   10-05 19:30:16.576:W / System.err(9074):at   java.lang.Thread.run(Thread.java:841)

0 个答案:

没有答案