【问题标题】:Progress indicator remains indeterminate and there is no download进度指示器仍然不确定,并且没有下载
【发布时间】:2017-03-01 16:27:57
【问题描述】:

我编写了一段代码,用于从 Internet 下载文件(在后台服务中)并在弹出阶段显示下载进度。代码编译成功,没有运行时错误。但是,不会进行下载,进度指示器仍然不确定。

代码是为说明我的观点而量身定制的。请看一下,让我明白我哪里出错了。

谢谢!

public class ExampleService extends Application {
URL url;    
Stage stage;

public void start(Stage stage)
{
    this.stage = stage;
    stage.setTitle("Hello World!");
    stage.setScene(new Scene(new StackPane(addButton()), 400, 200));
    stage.show();
}

private Button addButton()
{
    Button downloadButton = new Button("Download");
    downloadButton.setOnAction(new EventHandler<ActionEvent>()
    {
        public void handle(ActionEvent e)
        {
           FileChooser fileSaver = new FileChooser();
            fileSaver.getExtensionFilters().add(new FileChooser.ExtensionFilter("PDF", "pdf"));                

            File file = fileSaver.showSaveDialog(stage);

            getDownloadService(file).start();               
        }
    });        
    return downloadButton;
}

private Service getDownloadService(File file)
{
   Service downloadService = new Service() 
   {
       protected Task createTask()
       {
           return doDownload(file);
       }
   };

   return downloadService;
}

private Task doDownload(File file)
{
    Task downloadTask = new Task<Void>()
    {
       protected Void call() throws Exception
        {
            url = new URL("http://www.daoudisamir.com/references/vs_ebooks/html5_css3.pdf");

            // I have used this url for this context only
            org.apache.commons.io.FileUtils.copyURLToFile(url, file); 

            return null;
        } 
    };
    showPopup(downloadTask);
    return downloadTask;
}

Popup showPopup(Task downloadTask)
{
    ProgressIndicator progressIndicator = new ProgressIndicator();
    progressIndicator.progressProperty().bind(downloadTask.progressProperty());

    Popup progressPop = new Popup();
    progressPop.getContent().add(progressIndicator);
    progressPop.show(stage);

    return progressPop;

    // I have left out function to remove popup for simplicity
}
public static void main(String[] args)
{
    launch(args);
}}

【问题讨论】:

  • 如果有异常,你是不会知道的。向任务注册onFailed 处理程序:downloadTask.setOnFailed(e -&gt; downloadTask.getException().printStackTrace());。如果你想改变进度,你需要从你的call()方法调用updateProgress(...)

标签: java javafx


【解决方案1】:

行:

org.apache.commons.io.FileUtils.copyURLToFile(url, file);

...不向您提供有关下载进度的任何信息(没有回调或任何其他进度指示)。它只是下载一些东西而不给你反馈。

您将不得不使用其他东西来为您提供有关进度的反馈。

查看此问题的答案以获取带有反馈的解决方案(它适用于 Swing,但您应该能够将它们调整为 JavaFX):Java getting download progress

【讨论】:

  • 我查看了您的链接。我将不得不弄清楚如何使代码适应 JavaFX。代码使用 Java.io,而我想使用 org.apache.commons.io。使用起来更简单。
【解决方案2】:

您将ProgressIndicator 的progress 属性绑定到Task 的progress 属性,以便后者的更改将反映在前者中。然而你从来没有真正更新你的Task的进度

如果您希望进度指示器显示某些内容,则必须在任务主体(或其他地方)中调用 updateProgress(workDone, max)。如果您使用的下载逻辑没有给您任何进度回调,那可能会很棘手。 (也许,您可以生成一个线程来重复检查文件系统上文件的大小并将其用作您当前的工作完成;但是您需要知道文件的最终/完整大小是多少才能打开这变成一个百分比,这可能很容易,也可能不容易。)

【讨论】:

  • 我知道使用 updateProgress() 并找到最终的文件大小可能会很棘手。这就是为什么我需要更具体的回应。所有浏览器都实现了这个功能。
猜你喜欢
  • 1970-01-01
  • 2014-05-12
  • 2020-08-10
  • 1970-01-01
  • 1970-01-01
  • 2018-04-27
  • 1970-01-01
  • 1970-01-01
  • 2021-04-28
相关资源
最近更新 更多