使用java下载zip文件? [英] download zip file using java?

查看:127
本文介绍了使用java下载zip文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java从网页服务器下载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 =新字节[1024];
int count;

while((count = in.read(b))> 0){
out.write(b,0,count);
}
out.close();
in.close();

} catch(IOException e){
e.printStackTrace();
}
}


解决方案

应该是:



while((count = in.read(b))> = 0)



.read可以返回0。


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.

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();
    }
}

解决方案

It should be:

while ((count = in.read(b)) >= 0)

in.read can return 0.

这篇关于使用java下载zip文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆