4

我有一个应用程序可以从下拉框(这是公共共享路径)下载 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();
4

2 回答 2

1

似乎方法调用:

setDoOuput(true);

使请求成为 POST(请参阅 URLConnection.setDoOutput() 到底有什么影响?

删除它似乎可以解决问题。

于 2012-12-20T10:21:06.130 回答
0

尝试使用纯 URLConnection 而不是 HttpURLConnection。并查看输出。

于 2012-12-20T10:44:51.743 回答