我有一个应用程序可以从投递箱(这是公开共享的路径)下载 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/