我正在尝试使用commons net FTP 3.5和Java 1.8.0.45通过FTP重命名文件。有一个特定的文件夹,其中包含170K小文件(25 GB)。每当我尝试列出此文件夹时,它都会返回以下异常。对于其余文件夹,它运行正常并重命名文件。
org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:316)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:292)
at org.apache.commons.net.ftp.FTP.getReply(FTP.java:712)
at org.apache.commons.net.ftp.FTPClient.completePendingCommand(FTPClient.java:1857)
at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3420)
at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3335)
at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:3012)
at TestFTP.execute(TestFTP.java:27)
at TestFTP.main(TestFTP.java:12)
org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:316)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:292)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:503)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:628)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:602)
at org.apache.commons.net.ftp.FTP.quit(FTP.java:884)
at org.apache.commons.net.ftp.FTPClient.logout(FTPClient.java:1152)
at TestFTP.execute(TestFTP.java:62)
at TestFTP.main(TestFTP.java:12)
代码:
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class TestFTP {
public static void main(String[] args) {
TestFTP.execute(args[0], args[1]);
}
static DateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void execute(String ip, String folder) {
String server = ip;
int port = 21;
String user = "adminuser";
String pass = "adminuser";
long start = System.currentTimeMillis();
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
FTPFile[] files = ftpClient.listFiles(folder);
for (FTPFile file : files) {
String details = file.getName();
// renaming file
String oldFile = folder + file.getName();
String newFile = folder + "_X_" + file.getName();
boolean success = ftpClient.rename(oldFile, newFile);
if (success) {
System.out.println(oldFile + " was successfully renamed to: "
+ newFile);
} else {
System.out.println("Failed to rename: " + oldFile);
}
}
ftpClient.logout();
ftpClient.disconnect();
long end = System.currentTimeMillis();
System.out.println("time:" +(end-start));
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}
有没有办法让我的FTP服务器响应请求列出像增加超时这样的大文件夹?还是我错过了什么?提前谢谢!
答案 0 :(得分:0)
也许你的连接因为检索所有信息文件所花费的时间而关闭,你应该增加超时连接。
你应该玩超时,明智地使用FTPClient中的那些方法:
setDataTimeout(int timeout) //Sets the timeout in milliseconds to use when reading from the data connection.
setControlKeepAliveTimeout(long controlIdle)
//Set the time to wait between sending control connection keepalive messages when processing file upload or download.