Java.util.zip.GZIPInputStream.readFully when loading a file in android

I gave internet permission and when I tried to download the doc file from url I got java.io.EOFException In my case, when I hit the URL in the browser, it downloaded successfully on the PC, but it doesn’t work on my device.

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

+4

Source: https://habr.com/ru/post/1610276/


All Articles