下面的代码完全符合我的需要。我只需要让这个下载更多不同的URL(图片),遗憾的是我不知道该怎么做。如果有人能帮助我解决这个问题,我会非常感激。
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.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 {
URL url = new URL("http://www.drgarbage.com/img/howto/cfgf-tutorial/createcfg.png");
HttpURLConnection httpConnection = (HttpURLConnection) (url.openConnection());
long completeFileSize = httpConnection.getContentLength();
java.io.BufferedInputStream in = new java.io.BufferedInputStream(httpConnection.getInputStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream("picture.png");
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() {
ProgressBar.setValue(currentProgress);
}
});
bout.write(data, 0, x);
}
bout.close();
in.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
};
new Thread(updatethread).
start();
}
}