且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用java下载zip文件?

更新时间:2023-01-05 13:11:01

应该是:



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.