有任何一个例子,如何上传与ftp4j,支持简历,以及如何显示进度条?
发布于 2014-04-27 22:17:27
我刚刚实现了一种如下代码。
我发现如果使用压缩流,就不能依赖侦听器报告的传输字节,因为服务器可以等待进一步的数据来解码先前接收的块。
Hovewer,即使流是平原,在某些情况下失去连接时,您仍然不能依赖侦听器报告的总传输字节。因此,我终于意识到最好的方法是问服务器它接收了多少字节。
在我的模板中,时间冗余更普遍,涉及到与FTP服务器的控制连接。您可以将while循环限制在数据连接上,即上传。
FTPClient ftpClient = null;
long writtenBytes;
boolean isCompletedStartingDelete = false; // Our policy is overwrite at first
for (int attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
try {
ftpClient = getFTPClient();
configureFtpClient(ftpClient);
doLogin(ftpClient);
ftpClient.changeDirectory(remoteDirectory);
if (!isCompletedStartingDelete) { // Our policy is overwrite at first
try {
ftpClient.deleteFile(file);
isCompletedStartingDelete = true;
} catch (FTPException e) {
// Maybe you should check if this exception is really thrown for file not existing.
isCompletedStartingDelete = true;
}
}
try {
writtenBytes = ftpClient.fileSize(fileName);
} catch (Exception e) {
writtenBytes = 0;
}
if (ftpClient.isResumeSupported()) {
// With this template you also could use APPEND
ftpClient.upload(file, writtenBytes, listener);
} else {
ftpClient.upload(file, listener);
}
} catch (FTPAbortException e) {
// User Aborted operation
break;
} catch (Exception e) {
if (attempt == MAX_ATTEMPTS) { // Or in general lastLoop
throw e;
} else {
// Mask failure
// LOG
}
} finally {
if (ftpClient != null && ftpClient.isConnected()) {
try { ftpClient.disconnect(); } catch (Throwable t) { /* LOG */ }
}
}
https://stackoverflow.com/questions/5628126
复制相似问题