java 使用java下载zip文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2656209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
download zip file using java?
提问by Mohamed
I am downloading zip file from web server using Java but somehow I am loosing about 2kb in each file. I don't know why since same code works fine with other formats, e.g, text, mp3 and extra. any help is appreciated? here is my code.
我正在使用 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();
}
}
回答by Skip Head
It should be as below:
它应该如下所示:
while ((count = in.read(b)) >= 0)
in.readcan return 0.
in.read可以返回0。
回答by Ray
Put an out.flush()just after the " while ((count = in.read(b)) > 0) {...}" section and before the out.close().
把一个out.flush()刚刚“后while ((count = in.read(b)) > 0) {...}”部分和前out.close()。
回答by Maurice Perry
Try to remove the lines:
尝试删除这些行:
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("content-type", "binary/data");
回答by Bozhidar Batsov
I had a problem with downloading zip files from http once that turned out to be that my downloads included http headers in their beginning, but that made my files a bit larger not smaller, so you probably don't have this problem.
我曾经从 http 下载 zip 文件时遇到问题,结果证明我的下载在开始时包含 http 标头,但这使我的文件变大了而不是变小了,所以您可能没有这个问题。
As a side note you might consider using Apache Commons Netfor download related apps - it's really great.
作为旁注,您可能会考虑使用Apache Commons Net下载相关应用程序 - 这真的很棒。
回答by Stephen C
A few years ago I remember running into a problem with an old version of Tomcat (5.5.25 for memory) that would cause largish downloads to be truncated. We fixed this by upgrading to a 5.5.27. I also recall the same problem was found and fixed in an early Tomcat 6.0 release.
几年前,我记得在旧版本的 Tomcat(内存为 5.5.25)中遇到了一个问题,这会导致大量下载被截断。我们通过升级到 5.5.27 解决了这个问题。我还记得在早期的 Tomcat 6.0 版本中发现并修复了同样的问题。
If this rings any bells for you, take a look at the Tomcat change logs.
如果这为您敲响了警钟,请查看 Tomcat 更改日志。
回答by Kevin Day
Only zip files, huh? Very odd. Is it from any server, or just this one? If you rename the file (change extension) do you get the same problem? Which bytes are missing? Are you sure it's the last 2K bytes and not some chunk from the middle/etc... ?
只有 zip 文件,对吧?很奇怪。是来自任何服务器,还是仅此服务器?如果您重命名文件(更改扩展名),您会遇到同样的问题吗?缺少哪些字节?你确定它是最后的 2K 字节而不是来自中间/等的一些块......?

