Java 进度指示器仍不确定,无法下载

Java 进度指示器仍不确定,无法下载,java,javafx,Java,Javafx,我已经编写了一段代码,用于从internet(后台服务)下载文件,并在弹出阶段显示下载进度。代码编译成功,没有运行时错误。但是,没有下载,进度指示器仍然不确定 代码是为说明我的观点而定制的。请看一看,让我明白我错在哪里了 谢谢 public class ExampleService extends Application { URL url; Stage stage; public void start(Stage stage) { this.stage = stage;

我已经编写了一段代码,用于从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);
}}
公共类ExampleService扩展应用程序{
网址;
阶段性;
公众假期开始(阶段)
{
这个阶段=阶段;
舞台。片名(“你好,世界!”);
stage.setScene(新场景(新StackPane(addButton()),400200));
stage.show();
}
私有按钮addButton()
{
按钮下载按钮=新按钮(“下载”);
downloadButton.setOnAction(新的EventHandler()
{
公共无效句柄(ActionEvent e)
{
FileChooser fileSaver=新建FileChooser();
fileSaver.getExtensionFilters().add(新的FileChooser.ExtensionFilter(“PDF”,“PDF”);
File File=fileSaver.showsavedilog(stage);
getDownloadService(file.start();
}
});        
返回下载按钮;
}
私有服务getDownloadService(文件)
{
服务下载服务=新服务()
{
受保护的任务createTask()
{
返回下载(文件);
}
};
返回下载服务;
}
私有任务doDownload(文件)
{
任务下载任务=新任务()
{
受保护的Void调用()引发异常
{
url=新url(“http://www.daoudisamir.com/references/vs_ebooks/html5_css3.pdf");
//我仅将此url用于此上下文
org.apache.commons.io.FileUtils.copyURLToFile(url,文件);
返回null;
} 
};
showPopup(下载任务);
返回下载任务;
}
弹出显示弹出窗口(任务下载任务)
{
ProgressIndicator ProgressIndicator=新的ProgressIndicator();
progressIndicator.progressProperty().bind(downloadTask.progressProperty());
Popup progressPop=新的Popup();
progressPop.getContent().add(progressIndicator);
流行音乐表演(舞台);
返回进程POP;
//为了简单起见,我省略了删除弹出窗口的功能
}
公共静态void main(字符串[]args)
{
发射(args);
}}
行:

org.apache.commons.io.FileUtils.copyURLToFile(url,文件)

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

你将不得不使用其他的东西来给你进度反馈


看看这个问题答案,看看有反馈的解决方案(这是针对Swing的,但您应该能够将它们改编为JavaFX):

您将
ProgressIndicator
的进度属性绑定到
任务的进度属性,以便后者的更改将反映在前者中。但是,您从未实际更新
任务的进度


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

如果出现异常,您将不会知道。向任务注册
onFailed
处理程序:
downloadTask.setOnFailed(e->downloadTask.getException().printStackTrace())。如果您想更改进度,需要从
call()
方法调用。我已经查看了您的链接。我必须找出如何使代码适应JavaFX。代码使用Java.io,而我想使用org.apache.commons.io。我知道使用updateProgress()和查找最终文件大小可能很棘手。这就是为什么我需要更具体的回应。所有浏览器都实现此功能。