如何在javafx中下载时显示进度条

时间:2016-02-14 05:57:09

标签: java javafx javafx-2

我正在使用javafx构建桌面应用程序,我使用ftp下载大约500 MB的文件。 我需要在下载过程中显示%栏的进度条。 我还需要提供取消正在进行的下载过程的选项。

这是我下载文件的代码。

        try {
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            success = ftpClient.changeWorkingDirectory(PATH + preset + "/" +  file_to_download + offset);
            System.out.println("Download Path:-" + PATH + preset + "/" +  file_to_download + offset);
            if (!success) {
                System.out.println("Could not changed the directory to RIBS");
                return;
            } else {
                System.out.println("Directory changed to RIBS");
            }
            FTPFile[] files = ftpClient.listFiles();
            for (FTPFile file : files) {
                if (file.getName().contains(".zip")) {
                    dfile = file.getName();
                }

            }
            DirectoryChooser dirChooser = new DirectoryChooser();
            File chosenDir = dirChooser.showDialog(tableView.getScene().getWindow());
            System.out.println(chosenDir.getAbsolutePath());
            OutputStream output;
            output = new FileOutputStream(chosenDir.getAbsolutePath() + "/" + dfile);
            int timeOut = 500;
            ftpClient.setConnectTimeout(timeOut);
            if (ftpClient.retrieveFile(dfile, output) == true) {
                downloadButton.setDisable(true);
            }
            output.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

1 个答案:

答案 0 :(得分:0)

你应该熟悉Concurrency in JavaFX

您可以在网上找到几个关于您需要的例子,例如: G。 ProgressBar and Background Processes

相关问题