使用摆动按钮,我正在尝试下载一个 html 文件并将其写入一个新的 html 文件,同时做一个进度条。当我单击按钮时,我的程序似乎冻结了,直到下载完成,然后进度条突然变为 100%。我不太确定如何解决这个问题,因为我是 java 新手。
private void jButton2MouseReleased(java.awt.event.MouseEvent evt) {
try {
URL oracle = new URL("http://mywebsite.com");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
String input_path = "d:/website/updatedsite.html";
WriteFile data = new WriteFile(input_path, false);
int length = yc.getContentLength();
int current = 0;
jProgressBar1.setMaximum(length);
jProgressBar1.setValue(0);
while ((inputLine = in.readLine()) != null) {
data.writeToFile(inputLine);
int i = 0;
for (i = 0; i < length; i++) {
jProgressBar1.setValue(i);
}
}
in.close();
}
catch (java.io.IOException e) {
JOptionPane.showMessageDialog(Frame1.this, "Error " + e.getMessage());
}
}