我正在使用该循环从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);
}
答案 0 :(得分:3)
按照惯例使用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);
}
}
}
并将progressProperty()
的{{1}}绑定到Task
的{{1}}。
关于如何绑定两个属性的示例:
progressProperty()