【问题标题】:Apache Commons FTP problemsApache Commons FTP 问题
【发布时间】:2011-10-02 19:55:14
【问题描述】:

我想用 Apache Commons Net 实现一个 FTP 客户端,仅用于上传数据。 FTP 服务器的连接和登录工作正常。 但是上传不正常。 这些文件与原件相比有点大。 并且文件已损坏。 我尝试了图像、视频和文本文件。只有文本文件没问题。

现在我在调试时看到了

boolean tmp=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);

给我false。所以不能设置。为什么? (也许这不是问题?)

这是我的其余代码

client=new FTPClient();

    try {           
        int reply;
        client.connect(url, port);
        reply = client.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply))
        {
            client.disconnect();
            System.err.println("FTP server refused connection.");
            System.exit(1);
        }


        client.login(user, pw);
        boolean xxx=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
        client.setControlKeepAliveTimeout(300);
        client.enterLocalPassiveMode();

if (client.isConnected())
    {
    try {
        File file=new File(<FILE>);
        FileInputStream inputStream = new FileInputStream(file);
        OutputStream outputStream = client.storeFileStream(file.getName());

          byte[] buffer = new byte[4096];
          int l;
       while((l = inputStream.read(buffer))!=-1)
               {
                outputStream.write(buffer, 0, l);
            }

          inputStream.close();
          outputStream.flush();
          outputStream.close();}

【问题讨论】:

    标签: java ftp apache-commons-net


    【解决方案1】:

    如果只有文本文件传输成功,我怀疑你需要设置二进制传输文件类型。

    请参阅setFileType 方法以了解如何执行此操作。

    commons-net wiki 提到这是大多数文件损坏问题的原因。

    【讨论】:

    • 你好,我试过了,但是方法给我false,所以无法设置Mode。
    • 你在调用 connect 方法后的某个地方尝试过这个?
    • 还有登录方式之后。
    • 他尝试了setFileTransferMode,而不是您建议的setFileType 方法。这是一个正确的答案。
    【解决方案2】:

    更改以下内容:

    boolean xxx=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
    

    应该是:

    boolean xxx=client.setFileType(FTP.BINARY_FILE_TYPE);
    

    您将 FileTransferModes 与 FileTypes 混淆了。

    可用的文件类型有:

    可用的 FileTransferMode 有:

    我想如果 apache 为这些常量类型引入了枚举,那么可以避免这种问题,但是该库将无法用于 pre-java-5 运行时。
    我想知道 java 1.4 兼容性到底有多大问题。

    【讨论】:

      【解决方案3】:

      这对我有用,上传图片并在没问题后下载:使用

          FTP.LOCAL_FILE_TYPE
      

      此代码使用记录器,替换为记录器或使用 System.out.println("");

          private void cargarData(File filelocal) {
          FTPClient client = new FTPClient();
      
          try {
      
              client.connect("URLHOSTFTP", "PORT: DEFAULT 21");
              if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
                  client.disconnect();
                  logger.error("FTP server refused connection.");
                  System.exit(1);
              }
              client.login("USER FTP", "PASS FTP");
              boolean type = client.setFileType(FTP.LOCAL_FILE_TYPE);
      
              logger.info("Tipo Aceptado:" + type);
              client.setControlKeepAliveTimeout(300);
              client.enterLocalPassiveMode();
              if (client.isConnected()) {
                  FileInputStream fis = null;
                  fis = new FileInputStream(filelocal);
                  client.storeFile(filelocal.getName(), fis);
                  client.logout();
                  if (fis != null) {
                      fis.close();
                  }
              }
              logger.info(client.getReplyString());
          } catch (IOException e) {
              logger.error("error" + e.getMessage());
              e.printStackTrace();
      
          } catch (Exception e) {
              logger.error("error" + e.getMessage());
              e.printStackTrace();
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-08-09
        • 2012-03-20
        • 1970-01-01
        • 1970-01-01
        • 2013-06-01
        • 2015-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多