我正在尝试使用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服务器响应请求,像增加超时一样列出这么大的文件夹?还是我错过了什么?提前感谢!
https://stackoverflow.com/questions/38276160
复制