簡體   English   中英

使用Java下載Zip文件已損壞

[英]Downloading a Zip File with Java gets it corrupted

我只是嘗試從網站下載文件,我檢查了URL,並且那里的一切似乎都很好,但是當我嘗試按照說明進行操作時,它打印出我的更新大小為-1 Bytes ,不知道為什么這樣做正在發生。

如果有人有解決方案,我會很高興在這里解決。

我的代碼:

    private void downloadFile(String link) throws MalformedURLException, IOException
{
    URL url = new URL(link);
    URLConnection conn = url.openConnection();
    InputStream is = conn.getInputStream();
    long max = conn.getContentLength();
    outText.setText(outText.getText()+"\n"+"Downloding file...\nUpdate Size(compressed): "+max+" Bytes");
    System.out.println("Update Size --> " + max + " Bytes");
    BufferedOutputStream fOut = new BufferedOutputStream(new FileOutputStream(new File("update.zip")));
    byte[] buffer = new byte[32 * 1024];
    int bytesRead = 0;
    int in = 0;
    while ((bytesRead = is.read(buffer)) != -1) {
        in += bytesRead;
        fOut.write(buffer, 0, bytesRead);
    }
    fOut.flush();
    fOut.close();
    is.close();
    outText.setText(outText.getText()+"\nDownload Complete!");
}

非常感謝:)

Javadoc是您的朋友:

返回:此連接的URL引用的資源的內容長度; 如果未知內容長度,或者如果內容長度大於Integer.MAX_VALUE ,則返回-1

(強調我的)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM