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> {
@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) {
}
}
@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();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 20000);
OutputStream output = new FileOutputStream(downloadedPath);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
return downloadedPath;
}
protected void onProgressUpdate(String... progress) {
}
@Override
protected void onPostExecute(String file_url) {
try {
pDialog.dismiss();
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)