android - 在 Android 中下载 Zip 文件

标签 android download

我有一个应用程序可以从投递箱(这是公开共享的路径)下载 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, 0, 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 - 在 Android 中下载 Zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13807879/

相关文章:

java - 如何降低Android中录制的音频可视化的速度?

php - 下载文件前的javascript检查

c# - WebClient DownloadFileAsync 挂起

android - 更改 Realm 模型时出现 RealmMigrationNeededException

java - Android Canvas 和监听器

javascript - 浏览器超时 : the server at example. com 响应时间太长

ios - NSURLConnection 下载进度

安卓媒体播放器 : Download to File and Stream From File

android - 每当我尝试在 Android 设备上上传文件时出现 "No content provider"错误

java - Firebase 安卓。如果我多次添加同一个监听器怎么办?