我使用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