Java openConnection计算进度

时间:2013-07-25 15:39:43

标签: java

我有以下方法,我试图更新progressBar,了解下载进展的程度:

private void wget(java.net.URL url, String destination) throws MalformedURLException, IOException {
    java.net.URLConnection conn = url.openConnection();
    java.io.InputStream in = conn.getInputStream();

    File dstfile = new File(destination);
    OutputStream out = new FileOutputStream(dstfile);


    byte[] buffer = new byte[512];
    int length;
    int readBytes = 0;
    while ((length = in.read(buffer)) > 0) {
        out.write(buffer, 0, length);

        // Get progress
        int contentLength = conn.getContentLength();
        if (contentLength != -1) {

            //System.out.println((length / contentLength) * 100); ??
            UpdateForm.progressBar.setValue(2);
        } else {

        }            
    }

    in.close();
    out.close();
}

然而我似乎无法弄清楚如何计算已经消失了多少%..

有什么想法吗?

0 个答案:

没有答案