Java Swing应用程序的进度条

时间:2013-06-09 12:23:53

标签: java swing progress-bar

我已经创建了一个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();
        }
    }
});

1 个答案:

答案 0 :(得分:5)