Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Resume下载在Android中不工作大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
尽管在 Java应用程序中可以正常工作,但是在 Android中恢复下载的代码无法正常工作.在这里我试图下载一个zip文件,它将恢复下载,但最终的结果是无效的zip文件.
BufferedInputStream in = null;
        FiLeoutputStream fos = null;
        bufferedoutputstream bout=null;

        try {
            downloaded=0;
            httpURLConnection connection = (httpURLConnection) url.openConnection();
            if(ISSUE_DOWNLOAD_STATUs.intValue()==ECMConstant.ECM_DOWNLOADING){
                File file=new File(DESTinATION_PATH);
                if(file.exists()){
                    downloaded = (int) file.length();
                }
            }
            connection.setrequestProperty("Range","bytes=" + downloaded + "-");
            connection.connect();
            size=connection.getContentLength();
            Dialog.setMax(sizE);
             in = new BufferedInputStream(connection.geTinputStream());
             fos=(downloaded==0)? new FiLeoutputStream(DESTinATION_PATH): new FiLeoutputStream(DESTinATION_PATH,truE);
             bout = new bufferedoutputstream(fos,1024);
            byte[] data = new byte[1024];
            int x = 0;
            while ((x = in.read(data,1024)) >= 0) {
                bout.write(data,X);
                 downloaded += x;
                 System.out.println(downloaded);
                 onProgressupdate((int)(downloaded*100/sizE));
            }

            succes=true;
        } catch (Exception E) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
                bout.close();
            } catch (IOException E) {
                e.printStackTrace();
            }

        }

谢谢.

解决方法

httpURLConnection connection = (httpURLConnection) url.openConnection();
int buf = 1024;

if (ISSUE_DOWNLOAD_STATUs.intValue() == ECMConstant.ECM_DOWNLOADING) {
    File file = new File(DESTinATION_PATH);
    if (file.exists()) {
         downloaded = (int) file.length();
         connection.setrequestProperty("Range","bytes=" + file.length() + "-");
    }
} else {
    connection.setrequestProperty("Range","bytes=" + downloaded + "-");
}

connection.setDoInput(true);
connection.setDoOutput(true);

progressBar.setMax(connection.getContentLength());
in = new BufferedInputStream(connection.geTinputStream());
fos = new FiLeoutputStream(DESTinATION_PATH,downloaded == 0 ? false : truE);
bout = new bufferedoutputstream(fos,buf);
byte[] data = new byte[buf];

while ((int x = in.read(data,buf)) >= 0) {
    bout.write(data,X);
    downloaded += x;
    progressBar.setProgress(downloaded);
}

大佬总结

以上是大佬教程为你收集整理的Resume下载在Android中不工作全部内容,希望文章能够帮你解决Resume下载在Android中不工作所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。