下载时如何解决 JavaFX ProgressIndicator 重绘问题?
How to solve JavaFX ProgressIndicator repainting issues while downloading?
我正在使用那个循环从 ftp 客户端下载文件
private void download(String date){
try {
FTPFile ftpFile = client.mlistFile(filename());
long ftpFileSize = ftpFile.getSize();
frame.setMaximumProgress((int) ftpFileSize);
File downloadFile = new File(folder + filename());
OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(downloadFile));
InputStream inputStream = client.retrieveFileStream(fileName);
byte[] bytesArray = new byte[4096];
int bytesRead = -1;
int bytes = 0;
while ((bytesRead = inputStream.read(bytesArray)) != -1){
outputStream2.write(bytesArray, 0, bytesRead);
bytes += bytesRead;
}
if (client.completePendingCommand()){
outputStream2.close();
inputStream.close();
} else{
JOptionPane.showMessageDialog(null, "Downloading error.");
}
} catch (IOException e) {
if(e instanceof FTPConnectionClosedException){
reconnect();
}
}
}
ProgressIndicator 仍然在一个位置上。
方法 setProgress 成功打印了该进度。
public void setProgress(double value){
System.out.println(value);
progress.setProgress(value);
}
按原样使用Task Class,无需重新发明轮子..
public class DownloadTask extends Task<Void> {
public Void call() {
while ((bytesRead = inputStream.read(bytesArray)) != -1){
outputStream2.write(bytesArray, 0, bytesRead);
bytes += bytesRead;
updateProgress(bytes, ftpFileSize);
}
}
}
并将 Task
的 progressProperty()
绑定到 ProgressIndicator
的 progressProperty()
。
关于如何绑定两个属性的示例:
ProgressIndicator progress = new ProgressIndicator();
DownloadTask task = new DownloadTask();
progress.progressProperty().bind(task.progressProperty());
new Thread(task).start();
我正在使用那个循环从 ftp 客户端下载文件
private void download(String date){
try {
FTPFile ftpFile = client.mlistFile(filename());
long ftpFileSize = ftpFile.getSize();
frame.setMaximumProgress((int) ftpFileSize);
File downloadFile = new File(folder + filename());
OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(downloadFile));
InputStream inputStream = client.retrieveFileStream(fileName);
byte[] bytesArray = new byte[4096];
int bytesRead = -1;
int bytes = 0;
while ((bytesRead = inputStream.read(bytesArray)) != -1){
outputStream2.write(bytesArray, 0, bytesRead);
bytes += bytesRead;
}
if (client.completePendingCommand()){
outputStream2.close();
inputStream.close();
} else{
JOptionPane.showMessageDialog(null, "Downloading error.");
}
} catch (IOException e) {
if(e instanceof FTPConnectionClosedException){
reconnect();
}
}
}
ProgressIndicator 仍然在一个位置上。 方法 setProgress 成功打印了该进度。
public void setProgress(double value){
System.out.println(value);
progress.setProgress(value);
}
按原样使用Task Class,无需重新发明轮子..
public class DownloadTask extends Task<Void> {
public Void call() {
while ((bytesRead = inputStream.read(bytesArray)) != -1){
outputStream2.write(bytesArray, 0, bytesRead);
bytes += bytesRead;
updateProgress(bytes, ftpFileSize);
}
}
}
并将 Task
的 progressProperty()
绑定到 ProgressIndicator
的 progressProperty()
。
关于如何绑定两个属性的示例:
ProgressIndicator progress = new ProgressIndicator();
DownloadTask task = new DownloadTask();
progress.progressProperty().bind(task.progressProperty());
new Thread(task).start();