Android:“意外的流结束”异常下载大文件 [英] Android:"Unexpected end of stream" exception downloading large files

查看:301
本文介绍了Android:“意外的流结束”异常下载大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个Android应用程序,我需要从一个url下载一个文件,大小为33 MB。

I'm building an Android Application and I need to download a file from a url, which is 33 MB large.

这里下载任务:

try {
            int MAX_BUFFER_SIZE = 4096;
            URL mUrl = new URL(params[0]);
            HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
            connection.setRequestMethod("GET");
            long length = connection.getContentLength(), downloaded = 0;
            int read;
            byte [] buffer = new byte[(((int)length) > MAX_BUFFER_SIZE) ? MAX_BUFFER_SIZE : (int)length];
            String filename = getFilename(mUrl);
            File file = new File (SDCARD_ROOT);
            if (!file.exists() || !file.isDirectory()){
                file.mkdir();
            }
            this.filename = filename;
            file = new File (SDCARD_ROOT + this.filename);  
            FileOutputStream fos = new FileOutputStream (file);
            //Start downloading
            InputStream stream = connection.getInputStream();

            while ((read=stream.read(buffer)) > -1){
                fos.write(buffer, 0, read);
                downloaded += read;
                publishProgress((int) ((float) downloaded/length * 100));
            }
            fos.close();
            return 1;
        } catch (Exception e){
            Log.e("REV-PARTS", "Revolver parts error in DownloadTask: " + e.getMessage());  
            return 2;
        }

它适用于小文件(1-15 mb),但它会用大文件返回意外的流结束异常。
提前感谢

It works right with small files (1-15 mb), but it will return a "unexpected end of stream" exception with large files. Thanks in advance.

推荐答案

我有一个答案。
你应该添加一个标题到你的连接。

i have a answer for this. you should add a header to your connection.

connection.setRequestProperty("Accept-Encoding", "identity");

这将有助于你帮助我...

this will help you.say me if it help...

这篇关于Android:“意外的流结束”异常下载大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆