Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中下载文件时,java.util.zip.GZIPInputStream.readFully_Java_Android - Fatal编程技术网

在android中下载文件时,java.util.zip.GZIPInputStream.readFully

在android中下载文件时,java.util.zip.GZIPInputStream.readFully,java,android,Java,Android,我已经允许上网,并且 当我试图从url下载doc文件时,得到了java.io.EOFException 在我的例子中,当我在浏览器中点击url时,文档在pc中成功下载,但在我的设备上不起作用 class DownloadFileFromURL extends AsyncTask<String, String, String> { /** * Before starting background thread * Show Prog

我已经允许上网,并且 当我试图从url下载doc文件时,得到了java.io.EOFException 在我的例子中,当我在浏览器中点击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) {

            }

        }

    }
class DownloadFileFromURL扩展异步任务{
/**
*在启动后台线程之前
*显示进度条对话框
*/
@抑制警告(“弃用”)
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
试一试{
pDialog=newprogressdialog(getApplicationContext());
setMessage(“正在下载文件,请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
}捕获(异常anfe){
}
}
/**
*在后台线程中下载文件
*/
@凌驾
受保护的字符串doInBackground(字符串…f_url){
整数计数;
字符串downloadedPath=null;
试一试{
字符串urlStr=f_url[0];
字符串文件名=urlStr.substring(urlStr.lastIndexOf('/')+1);
URL url1=新URL(urlStr);
URI=新URI(url1.getProtocol()、url1.getUserInfo()、url1.getHost()、url1.getPort()、url1.getPath()、url1.getQuery()、url1.getRef());
url1=uri.toURL();
字符串encodeUrl=url1.toString();
最终字符串扩展名=android.webkit.MimeTypeMap.getFileExtensionFromUrl(urlStr);
File File=新文件(Environment.getExternalStorageDirectory()+“/File/download”);
如果(!file.exists()){
mkdirs()文件;
}
downloadedPath=file.getAbsolutePath()+“/”+md5String+“+”+扩展名;
URL URL=新URL(编码URL);
URLConnection conconnection=url.openConnection();
setRequestProperty(“接受编码”,“接受编码”);
conconnect.connect();
//获取文件长度
int lenghtOfFile=conconnect.getContentLength();
//读取文件的输入流-带8k缓冲区
InputStream输入=新的BufferedInputStream(url.openStream(),20000);
//输出流以写入文件
OutputStream output=新文件OutputStream(下载路径);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
//发布进度。。。。
//在此之后,将调用onProgressUpdate
出版进度(“+(int)((总计*100)/长度文档));
//将数据写入文件
输出.写入(数据,0,计数);
}
//冲洗输出
output.flush();
//合流
output.close();
input.close();
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回下载路径;
}
/**
*更新进度条
*/
受保护的void onProgressUpdate(字符串…进度){
//设置进度百分比
//pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
*完成后台任务后
*关闭进度对话框
* *
*/
@凌驾
受保护的void onPostExecute(字符串文件\u url){
//下载文件后关闭对话框
试一试{
pDialog.disclose();
//在图像视图中显示下载的图像
//从SD卡读取图像路径
如果(文件url!=null){
openDocument(文件\ url);
}
}捕获(异常anfe){
}
}
}
10-05 19:30:16.576:W/System.err(9074):java.io.EOFException 10-05 19:30:16.576:W/系统错误(9074):在 java.util.zip.GZIPInputStream.readFully(GZIPInputStream.java:202) 10-05 19:30:16.576:W/系统错误(9074):在 java.util.zip.GZIPInputStream.(GZIPInputStream.java:98)10-05 19:30:16.576:W/系统错误(9074):在 java.util.zip.GZIPInputStream.(GZIPInputStream.java:81)10-05 19:30:16.576:W/系统错误(9074):在 com.android.okhttp.internal.http.HttpEngine.initContentStream(HttpEngine.java:468) 10-05 19:30:16.576:W/系统错误(9074):在 com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:666) 10-05 19:30:16.576:W/系统错误(9074):在 com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347) 10-05 19:30:16.576:W/系统错误(9074):在 com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296) 10-05 19:30:16.576:W/系统错误(9074):在 com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179) 10-05 19:30:16.576:W/系统错误(9074):在 java.net.URL.openStream(URL.java:470)10-0519:30:16.576: 带系统错误(9074):在 demo.openfile.MainActivity$DownloadFileFromURL.doInBackground(MainActivity.java:129) 10-05 19:30:16.576:W/系统错误(9074):在 demo.openfile.MainActivity$DownloadFileFromURL.doInBackground(MainActivity.java:1) 10-05 19:30:16.576:W/系统错误(9074):在 android.os.AsyncTask$2。