【发布时间】:2018-12-17 19:18:53
【问题描述】:
我正在尝试从我的 ftp 服务器下载文件,但在检索文件时它卡住了。我正在使用commons-net-3.6.jar
我注意到的事情
当我使用ftpClient.enterRemotePassiveMode();时
我可以在FileZilla服务器界面看到连接下载进度卡在76%(688,128字节)
当我使用ftpClient.enterLocalPassiveMode();
我可以在 FileZilla 服务器界面中看到连接的下载进度卡在 96%(884,736 字节)
有趣的是,在这两种模式下,无论文件是什么,它总是卡在完全相同的字节数上。如果文件大小大于 884,736 或 688,128,它就会卡住。
它非常适用于 LocalPassiveMode 中小于 884,736 字节和 RemotePassiveMode 中小于 688,128 字节的文件。
我尝试从其他服务器下载,但没有成功,因此绝对不是服务器相关问题。
我的代码
public class FTPDownload {
private List<BufferedImage> imageList;
private boolean wasConnected;
public void getFiles(String orderRootDirectory){
wasConnected = true;
imageList = new ArrayList<>();
FTPConnection con = new FTPConnection(); // to get the FTP Credentials
con.readData();
try {
FTPClient ftpClient = new FTPClient();
ftpClient.connect(con.getServerIp());
ftpClient.enterLocalPassiveMode();
ftpClient.login(con.getUsername(), con.getPassword());
ftpClient.setAutodetectUTF8(true);
ftpClient.setBufferSize(1024 * 1024); // tried with and without this no luck there
wasConnected = ftpClient.isConnected();
FTPFile[] files = ftpClient.listFiles(orderRootDirectory);
for (FTPFile file : files) {
String details = file.getName();
if (file.isDirectory()) {
details = "[" + details + "]";
}
String totalFilePath = orderRootDirectory+"/"+file.getName();
InputStream inputStream = ftpClient.retrieveFileStream(totalFilePath); // stuck over here
System.out.println(ftpClient.completePendingCommand());
System.out.println(ftpClient.getReplyString());
System.out.println("Reading File...");
long start=System.currentTimeMillis();
ImageIO.setUseCache(false);
BufferedImage bimg = ImageIO.read(inputStream);
long end=System.currentTimeMillis();
System.out.println("time="+(end-start));
imageList.add(bimg);
details += "\t\t" + file.getSize();
details += "\t\t" + file.getName();
System.out.println(details);
}
System.out.println(imageList.size());
ftpClient.logout();
ftpClient.disconnect();
}catch (Exception e){
e.printStackTrace();
wasConnected = false;
}
}
【问题讨论】:
标签: java ftp file-transfer ftp-client apache-commons-net