java下载进度怎样判断_Java获得下载进度
⼀个⼯作⽰例,使⽤您的代码并在进度条中显⽰进度可能如下所⽰:
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.HttpURLConnection;
import java.URL;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
表单大师如何设置是否孙侨潞是把她扔进海里了吗public class Progressbar {
public static void main(String[] args) {
final JProgressBar jProgressBar = new JProgressBar();
jProgressBar.setMaximum(100000);
JFrame frame = new JFrame();
frame.setContentPane(jProgressBar);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(300, 70);
frame.setVisible(true);
Runnable updatethread = new Runnable() {
public void run() {
try {
安卓8游戏框架URL url = new URL("downloads.sourceforge/project/bitcoin/Bitcoin/blockchain/bitcoin_blockchain_170000.zip"); HttpURLConnection httpConnection = (HttpURLConnection) (url.openConnection());
long completeFileSize = ContentLength();
java.io.BufferedInputStream in = new java.io.InputStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream(
"package.zip");
java.io.BufferedOutputStream bout = new BufferedOutputStream(
fos, 1024);
byte[] data = new byte[1024];
long downloadedFileSize = 0;
int x = 0;
while ((x = in.read(data, 0, 1024)) >= 0) {
downloadedFileSize += x;
// calculate progress
final int currentProgress = (int) ((((double)downloadedFileSize) / ((double)completeFileSize)) * 100000d); // update progress bar
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
jProgressBar.setValue(currentProgress);
}
});
mysql workbench报错bout.write(data, 0, x);
递归算法可以无限制调用自身}
bout.close();
in.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
};
new Thread(updatethread).
start();
java下载过程}
}
这显⽰了如何做到 – 在使⽤之前你应该考虑GUI-Threads等.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论