我有以下方法,我试图更新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();
}
然而我似乎无法弄清楚如何计算已经消失了多少%..
有什么想法吗?