在android中下载Zip文件

在android中下载Zip文件,第1张

概述我有一个应用程序从下拉框(公共共享路径)下载zip文件内容.我使用HttpURLConnection编写下载代码,但它没有按预期工作,而是下载一小部分(下载zip文件显示31 kb但其原始大小为3mb).我正在附上我的代码.请帮我解决这个问题. URL url = new URL("drop box public share url"); //create the new conn 我有一个应用程序从下拉框(公共共享路径)下载zip文件内容.我使用httpURLConnection编写下载代码,但它没有按预期工作,而是下载一小部分(下载zip文件显示31 kb但其原始大小为3mb).我正在附上我的代码.请帮我解决这个问题.

URL url = new URL("drop Box public share url");        //create the new connection        httpURLConnection urlConnection = (httpURLConnection) url.openConnection();        urlConnection.setAllowUserInteraction(false);        urlConnection.setInstanceFollowRedirects(true);        urlConnection.setConnectTimeout(5 * 1000);        urlConnection.setRequestMethod("GET");        urlConnection.setDoOutput(true);        urlConnection.setDoinput(true);        urlConnection.connect();        file SDCardRoot = Environment.getExternalStorageDirectory();        file file = new file(SDCardRoot,"/download/sample.zip");        fileOutputStream fileOutput = new fileOutputStream(file);        inputStream inputStream = urlConnection.getinputStream();        int totalSize = urlConnection.getContentLength();        int downloadedSize = 0;        //create a buffer...        byte[] buffer = new byte[1024];        int bufferLength = 0;         while ( (bufferLength = inputStream.read(buffer)) > 0 ) {                fileOutput.write(buffer,bufferLength);                                    downloadedSize += bufferLength;                onProgressUpdate(downloadedSize,totalSize);        }        //close the output stream when done        fileOutput.close();        inputStream.close();
解决方法 似乎方法调用:

setDoOuput(true);

使请求成为POST(请参阅
What exactly does URLConnection.setDoOutput() affect?)

删除它似乎解决了这个问题.

总结

以上是内存溢出为你收集整理的在android中下载Zip文件全部内容,希望文章能够帮你解决在android中下载Zip文件所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1124763.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存