【问题标题】:Java FTPClient StuckJava FTPClient 卡住了
【发布时间】: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


    【解决方案1】:

    FTPClient#retrieveFileStream 声明:

    当你读完 InputStream 时,你必须关闭它。这 InputStream 本身将负责关闭父数据 关闭时的连接套接字。

    要完成文件传输,您必须调用 completePendingCommand 和 检查其返回值以验证成功。如果不这样做, 后续命令的行为可能出乎意料


    调用应该finalized使用completePendingCommand,现在你正在执行那个方法before finalizing(你读到inputStream之后你已经称为completePendingCommand)。

    您也没有关闭它特别声明的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
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-14
      • 2012-06-23
      • 2021-10-08
      • 2016-11-24
      • 1970-01-01
      相关资源
      最近更新 更多