gpt4 book ai didi

java - Java Swing 应用程序的进度条

转载 作者:搜寻专家 更新时间:2023-11-01 02:27:57 24 4
gpt4 key购买 nike

我已经创建了一个 Java swing 应用程序,用于使用 Java servlet 下载文件。

客户端机器有一个 GUI,它提供了一个要下载的文件列表。

如何设置进度条来监控文件下载?

这是客户端机器的代码:

b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
HttpClient client = new DefaultHttpClient();
String value = comboBox1.getSelectedItem().toString();

HttpGet post = new HttpGet(
"http://localhost:8080/may16/FileDownloadServletNormal?value="
+ value + "&type=normal");
try {
LocalTime time1 = new LocalTime();

// executing the POST request
HttpResponse rsp = client.execute(post);
// reading response data
HttpEntity entity = rsp.getEntity();

InputStream inputStream = entity.getContent();

OutputStream outputStream = new FileOutputStream(
new File("D://temp//downloaded//" + value));

IOUtils.copy(inputStream, outputStream);

/*try {
Thread.sleep(25000);
}
catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}*/

outputStream.flush();
outputStream.close();
LocalTime time2 = new LocalTime();
Seconds sec = Seconds.secondsBetween(time1, time2);
System.out.println("Time Taken:" + sec.getSeconds() + "secs");
//normalDownloadTime.setText("Normal download time:"+sec.getSeconds());
normalDownloadTime.setText("Normal download time:"
+ sec.getSeconds() + "secs");
// String response = convertStreamToString(inputStream);
// System.out.println(response);

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});

最佳答案

关于java - Java Swing 应用程序的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17009570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com