我使用apache的FTPClient从FTP服务器下载文件。我的情况是 - FTP服务器可能会松动网络连接,并可能在最多1天内保持断开状态。当它连接回来时,文件的下载应该从剩下的地方开始。我使用下面的代码连接到服务器,然后从服务器下载文件使用apache FTPClient从FTP服务器下载文件
public void retrieve(String server, int port, String username,
String password, String remote, String local, int fileType,
ProgressHandler progressHandler) throws Exception {
final FTPClient ftp = new FTPClient();
Date date = new Date();
long startTime_ms = date.getTime();
if (progressHandler != null) {
ftp.setCopyStreamListener(new FtpCopyStreamListener(progressHandler));
}
ftpConnect(server,ftp, port,startTime_ms);
if(ftp.getReplyCode()==0 || !String.valueOf(ftp.getReplyCode()).startsWith("2")){
cleanup(ftp, "Could not log into server: " + server, null);
return;
}
boolean loggedIn = false;
try {
if (username == null || username.isEmpty()) {
username = "anonymous";
password = System.getProperty("user.name") + "@"
+ InetAddress.getLocalHost().getHostName();
}
if (!ftp.login(username, password)) {
ftp.logout();
cleanup(ftp, "Could not log into server: " + server, null);
}
loggedIn = true;
ftp.setFileType(fileType);
ftp.enterLocalPassiveMode();
OutputStream output = null;
try {
output = new FileOutputStream(local);
LOGGER.info("About to download " + remote + " from " + server
+ " on " + (port > 0 ? port : ftp.getDefaultPort())
+ " to " + local);
ftp.setControlKeepAliveTimeout(300);
boolean isFileDownloaded = false;
try{
isFileDownloaded = ftp.retrieveFile(remote, output);
}
catch(IOException ex){
if(ftp.isConnected()){
try{
int retryCode = 0;
while (!String.valueOf(retryCode).startsWith("2") && (new Date().getTime() < (startTime_ms + TOTAL_DURATION_MSECS))){
try{
retryCode = ftp.retr(local);
}catch(Exception e1){
Thread.sleep(1000);
System.out.println("File not downloaded !! " + e1.toString());
ftpConnect(server, ftp, port, startTime_ms);
}
}
}catch(Exception e){
//ftp.getReplyCode()
//throw e;
//disconnect(ftp);
//ftpConnect(server, ftp, port, startTime_ms);
System.out.println("File not downloaded !! " + e.toString());
}
}
}
if(isFileDownloaded){
LOGGER.info("Finished downloading " + remote + " from "
+ server + " on "
+ (port > 0 ? port : ftp.getDefaultPort()) + " to "
+ local);
}
else{
System.out.println("File not downloaded !! ");
}
}catch(IOException io){
io.printStackTrace();
throw io;
}
finally {
if (output != null) {
try {
output.close();
} catch (IOException f) {
LOGGER.severe("output.close() error: "
+ ServiceUtils.stackToString(f));
}
}
}
}
catch (FTPConnectionClosedException e) {
LOGGER.severe("Server closed connection: " + server + " "
+ ServiceUtils.stackToString(e));
throw e;
} catch (IOException e) {
LOGGER.severe("IOException, server: " + server + " "
+ ServiceUtils.stackToString(e));
throw e;
} finally {
if (loggedIn) {
try {
ftp.logout();
} catch (IOException f) {
LOGGER.severe("ftp.logout() error: "
+ ServiceUtils.stackToString(f));
}
}
disconnect(ftp);
}
}
我的问题基本上是如果retrieveFile()方法失败没有任何反正我可以重新连接并开始从点下载它被断开。
现在我正在使用Microsoft IIS服务器作为我的FTP服务器,但在生产环境中它将成为FileZilla FTP服务器。
任何帮助表示赞赏。 谢谢
见[重启()](http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient。html#restart%28long%29),因为它工作的服务器应该支持'REST'命令 – fvu 2014-09-26 20:42:05
@fvu我试过rest()方法,但它并没有启动文件下载恢复过程。你能分享一些代码片段吗? – Anirban 2014-09-26 22:37:08