download zip file using java?

前端 未结 6 1428
鱼传尺愫
鱼传尺愫 2021-02-15 12:13

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,

相关标签:
6条回答
  • 2021-02-15 12:39

    Put an out.flush() just after the " while ((count = in.read(b)) > 0) {...}" section and before the out.close().

    0 讨论(0)
  • 2021-02-15 12:42

    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.

    If this rings any bells for you, take a look at the Tomcat change logs.

    0 讨论(0)
  • 2021-02-15 12:46

    It should be as below:

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

    in.read can return 0.

    0 讨论(0)
  • 2021-02-15 12:52

    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.

    As a side note you might consider using Apache Commons Net for download related apps - it's really great.

    0 讨论(0)
  • 2021-02-15 12:52

    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... ?

    0 讨论(0)
  • 2021-02-15 12:54

    Try to remove the lines:

    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setRequestProperty("content-type", "binary/data");
    
    0 讨论(0)
提交回复
热议问题