当前位置: 首页 > 面试题库 >

在下载文件时使用swing GUI显示进度条

富凯风
2023-03-14
问题内容

我目前有一个班级,应该在下载文件时向我展示一个简单的表格。它正在工作,但是进度条没有更新,下载完成后我只能看到它。有人能帮我吗?

import java.awt.FlowLayout;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JProgressBar;

public class Downloader {

    public Downloader(String site, File file) {
        JFrame frm = new JFrame();
        JProgressBar current = new JProgressBar(0, 100);
        current.setSize(50, 100);
        current.setValue(0);
        current.setStringPainted(true);
        frm.add(current);
        frm.setVisible(true);
        frm.setLayout(new FlowLayout());
        frm.setSize(50, 100);
        frm.setDefaultCloseOperation(EXIT_ON_CLOSE);
        try {
            URL url = new URL(site);
            HttpURLConnection connection
                    = (HttpURLConnection) url.openConnection();
            int filesize = connection.getContentLength();
            float totalDataRead = 0;
            try (java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream())) {
                java.io.FileOutputStream fos = new java.io.FileOutputStream(file);
                try (java.io.BufferedOutputStream bout = new BufferedOutputStream(fos, 1024)) {
                    byte[] data = new byte[1024];
                    int i;
                    while ((i = in.read(data, 0, 1024)) >= 0) {
                        totalDataRead = totalDataRead + i;
                        bout.write(data, 0, i);
                        float Percent = (totalDataRead * 100) / filesize;
                        current.setValue((int) Percent);
                    }
                }
            }
        } catch (IOException e) {
            javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null, e.getMessage(), "Error",
                    javax.swing.JOptionPane.DEFAULT_OPTION);
        }
    }
}

问题答案:

您的是经典的Swing并发问题。解决方案始终如一-在后台线程中运行长时间的代码,例如可以在SwingWorker中找到的代码。

例如,

import java.awt.FlowLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutionException;

import javax.swing.JFrame;
import javax.swing.SwingWorker;

import static javax.swing.JFrame.EXIT_ON_CLOSE;

import javax.swing.JProgressBar;

public class Downloader {

   public Downloader(String site, File file) {
      JFrame frm = new JFrame();
      final JProgressBar current = new JProgressBar(0, 100);
      current.setSize(50, 100);
      current.setValue(0);
      current.setStringPainted(true);
      frm.add(current);
      frm.setVisible(true);
      frm.setLayout(new FlowLayout());
      frm.setSize(50, 100);
      frm.setDefaultCloseOperation(EXIT_ON_CLOSE);
      final Worker worker = new Worker(site, file);
      worker.addPropertyChangeListener(new PropertyChangeListener() {

         @Override
         public void propertyChange(PropertyChangeEvent pcEvt) {
            if ("progress".equals(pcEvt.getPropertyName())) {
               current.setValue((Integer) pcEvt.getNewValue());
            } else if (pcEvt.getNewValue() == SwingWorker.StateValue.DONE) {
               try {
                  worker.get();
               } catch (InterruptedException | ExecutionException e) {
                  // handle any errors here
                  e.printStackTrace(); 
               }
            }

         }
      });
      worker.execute();
   }
}

class Worker extends SwingWorker<Void, Void> {
   private String site;
   private File file;

   public Worker(String site, File file) {
      this.site = site;
      this.file = file;
   }

   @Override
   protected Void doInBackground() throws Exception {
      URL url = new URL(site);
      HttpURLConnection connection = (HttpURLConnection) url
            .openConnection();
      int filesize = connection.getContentLength();
      int totalDataRead = 0;
      try (java.io.BufferedInputStream in = new java.io.BufferedInputStream(
            connection.getInputStream())) {
         java.io.FileOutputStream fos = new java.io.FileOutputStream(file);
         try (java.io.BufferedOutputStream bout = new BufferedOutputStream(
               fos, 1024)) {
            byte[] data = new byte[1024];
            int i;
            while ((i = in.read(data, 0, 1024)) >= 0) {
               totalDataRead = totalDataRead + i;
               bout.write(data, 0, i);
               int percent = (totalDataRead * 100) / filesize;
               setProgress(percent);
            }
         }
      }
      return null;
   }
}


 类似资料:
  • 问题内容: 我正在尝试使用Ajax下载文件并显示自定义 下载进度栏。 问题是我不知道该怎么做。我编写了用于记录进度的代码,但不知道如何启动下载。 注意: 文件是不同类型的。 提前致谢。 JS HTML和PHP 问题答案: 如果要向用户显示下载过程的进度条,则必须在xmlhttprequest中进行下载。这里的问题之一是,如果您的文件很大-它们将被保存 在 浏览器 的内存 中,然后浏览器会将它们写入

  • 我使用下载管理器api下载大文件,我也实现了。但问题是,下载进度通知显示在状态栏中。但是我想用进度条在活动中显示通知。有人做过吗,如果有请帮帮我。

  • 我正在尝试写一个简单的应用程序得到更新。为此,我需要一个简单的函数,可以下载一个文件,并在中显示当前的进度。我知道如何执行,但我不确定如何显示当前进度以及如何首先下载文件。

  • 本文向大家介绍Python 给下载文件显示进度条和下载时间的实现,包括了Python 给下载文件显示进度条和下载时间的实现的使用技巧和注意事项,需要的朋友参考一下 大家在下载文件时能够显示下载进度和时间非常好,其实实现它方法很简单,这里我写了个进度条的模块,其中还附带上了运行时间也就是下载时间了。 该模块调用了三个库: 1.os 2.requests 3.time 话不多说,先上代码!!!. 实现

  • 问题内容: 我正在使用javafx构建桌面应用程序,我正在使用ftp下载约500 MB的文件。下载进行时,我需要在进度条上显示%。我还需要提供一个取消正在进行的下载过程的选项。 这是我下载文件的代码。 问题答案: 您应该使自己熟悉JavaFX中的并发性。 您可以在网上找到一些有关所需内容的示例,例如ProgressBar和Background Processes 。

  • 我的日志工作正常,而我的下载正在运行,显示“下载正在进行!”当它完成“下载完成!”,但同样的情况并没有发生在我的进程中,为什么?我真的需要一些帮助,其他的逻辑来做到这一点真的很感激