我正在使用Java从Web服务器下载zip文件,但是每个文件都会丢失大约2kb。我不知道为什么,因为相同的代码在其他格式(如文本、mp3和额外格式)中正常工作。任何帮助都将不胜感激。 以下是我的代码。
public void download_zip_file(String save_to) {
try {
URLConnection conn = this.url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("content-type", "binary/data");
InputStream in = conn.getInputStream();
FileOutputStream out = new FileOutputStream(save_to + "tmp.zip");
byte[] b = new byte[1024];
int count;
while ((count = in.read(b)) > 0) {
out.write(b, 0, count);
}
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}