Java FTP客户端卡住了
我正试图从我的ftp服务器下载一个文件,但它在检索文件时卡住了。我使用的是Java FTP客户端卡住了,java,ftp,file-transfer,ftp-client,apache-commons-net,Java,Ftp,File Transfer,Ftp Client,Apache Commons Net,我正试图从我的ftp服务器下载一个文件,但它在检索文件时卡住了。我使用的是commons-net-3.6.jar 我注意到的事情 当我使用ftpClient.enterRemotePassiveMode()时我可以在FileZilla服务器界面中看到,连接的下载进度停留在76%(688128字节) 当我使用ftpClient.enterLocalPassiveMode()时我可以在FileZilla服务器界面中看到,连接的下载进度停留在96%(884736字节) 有趣的是,在这两种模式中,无论文
commons-net-3.6.jar
我注意到的事情
当我使用ftpClient.enterRemotePassiveMode()时代码>
我可以在FileZilla服务器界面中看到,连接的下载进度停留在76%(688128字节)
当我使用ftpClient.enterLocalPassiveMode()时代码>
我可以在FileZilla服务器界面中看到,连接的下载进度停留在96%(884736字节)
有趣的是,在这两种模式中,无论文件是什么,它总是停留在完全相同的字节数上。如果文件大小大于884736或688128,它就会卡住
它适用于LocalPassiveMode中小于884736字节和RemotePassiveMode中小于688128字节的文件
我试过从另一台服务器下载,但它不起作用,所以它肯定不是与服务器相关的问题
我的代码
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;
}
}
公共类FTPDownload{
私人名单;
私有布尔连接;
公共void getFiles(字符串orderRootDirectory){
wasconned=true;
imageList=新的ArrayList();
FTPConnection con=new FTPConnection();//获取FTP凭据
con.readData();
试一试{
FTPClient FTPClient=新的FTPClient();
ftpClient.connect(con.getServerIp());
ftpClient.enterLocalPassiveMode();
ftpClient.login(con.getUsername(),con.getPassword());
ftpClient.setAutoDetecturtf8(真);
ftpClient.setBufferSize(1024*1024);//尝试使用和不使用此选项都不会带来好运
wasconned=ftpClient.isConnected();
FTPFile[]files=ftpClient.listFiles(orderRootDirectory);
用于(FTPFile文件:文件){
字符串详细信息=file.getName();
if(file.isDirectory()){
详细信息=“[”+详细信息+“]”;
}
字符串totalFilePath=orderRootDirectory+“/”+file.getName();
InputStream InputStream=ftpClient.retrieveFileStream(totalFilePath);//卡在这里
System.out.println(ftpClient.completePendingCommand());
System.out.println(ftpClient.getReplyString());
System.out.println(“读取文件…”);
长启动=System.currentTimeMillis();
ImageIO.setUseCache(false);
BuffereImage bimg=图像IO.read(inputStream);
long end=System.currentTimeMillis();
System.out.println(“time=“+(end-start));
图像列表。添加(bimg);
详细信息+=“\t\t”+文件.getSize();
详细信息+=“\t\t”+文件.getName();
系统输出打印项次(详细信息);
}
System.out.println(imageList.size());
ftpClient.logout();
ftpClient.disconnect();
}捕获(例外e){
e、 printStackTrace();
wasconned=false;
}
}
声明:
您必须在完成对InputStream的读取后关闭它
InputStream本身将负责关闭父数据
连接插座关闭时
及
要完成文件传输,必须调用completePendingCommand和
检查其返回值以验证是否成功。如果未执行此操作,
后续命令的行为可能会出乎意料
调用应该使用completePendingCommand
完成,现在您正在执行该方法,然后再完成(在调用completePendingCommand
之后,您正在从inputStream
读取)
此外,您没有关闭它特别声明的inputStream
要解决您的问题,请执行以下操作:首先关闭inputStream
,然后调用completePendingCommand
,所有这些都应该在阅读inputStream
之后发生
InputStream inputStream = ftpClient.retrieveFileStream(totalFilePath);
BufferedImage bimg = ImageIO.read(inputStream);
inputStream.close();
if (!ftpClient.completePendingCommand()) {
// Throw some error or do something, file transfer failed
}
声明:
您必须在完成对InputStream的读取后关闭它
InputStream本身将负责关闭父数据
连接插座关闭时
及
要完成文件传输,必须调用completePendingCommand和
检查其返回值以验证是否成功。如果未执行此操作,
后续命令的行为可能会出乎意料
调用应该使用completePendingCommand
完成,现在您正在执行该方法,然后再完成(在调用completePendingCommand
之后,您正在从inputStream
读取)
此外,您没有关闭它特别声明的inputStream
要解决您的问题,请执行以下操作:首先关闭inputStream
,然后调用completePendingCommand
,所有这些都应该在阅读inputStream
之后发生
InputStream inputStream = ftpClient.retrieveFileStream(totalFilePath);
BufferedImage bimg = ImageIO.read(inputStream);
inputStream.close();
if (!ftpClient.completePendingCommand()) {
// Throw some error or do something, file transfer failed
}