java - 在java中使用ftp下载时文件被损坏

标签 java ftp file-transfer

当我尝试提取压缩文件时,文件被损坏。这是我使用过的代码。我无法理解我出了什么问题。文件的格式是.zip,里面有XML文件。从远程服务器下载后,XML 文件标签发生更改并且看起来已损坏。

public Boolean pullConfirmationsFTP(String host, String sftpUserName, String sftpPwd,
      String sftpPort, String fromConfirmationDirectory, String archiveConfirmationDirectory,
      String toDirectory) {
    try {

      // new ftp client
      FTPClient ftp = new FTPClient();
      // try to connect
      ftp.connect(host);
      // login to server
      if (!ftp.login(sftpUserName, sftpPwd)) {
        ftp.logout();
        LOG4J.error("Authentication failed");
      }
      int reply = ftp.getReplyCode();
      // FTPReply stores a set of constants for FTP reply codes.
      if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
      }

      // enter passive mode
      ftp.enterLocalPassiveMode();

      // get system name
      // System.out.println("Remote system is " + ftp.getSystemType());
      // change current directory
      ftp.changeWorkingDirectory(fromConfirmationDirectory);
      System.out.println("Current directory is " + ftp.printWorkingDirectory());

      // get list of filenames
      FTPFile[] ftpFiles = ftp.listFiles();

      if (ftpFiles != null && ftpFiles.length > 0) {
        // loop thru files
        for (FTPFile file : ftpFiles) {
          try{
          if (!file.isFile()) {
            continue;
          }
          LOG4J.error("File is " + file.getName());
          // get output stream
          OutputStream output;
          output = new FileOutputStream(toDirectory + file.getName());
          // get the file from the remote system
          ftp.retrieveFile(file.getName(), output);

          // close output stream
          output.close();

          // delete the file
          ftp.deleteFile(file.getName());
          }
          catch(Exception e)
          {
            LOG4J.error("Error in pushing file : ",e);
          }
        }
      }

      ftp.logout();
      ftp.disconnect();
    } catch (Exception ex) {
      ex.printStackTrace();
      LOG4J.error(ex);
    }

    return true;
  }

最佳答案

你没有说你使用哪个 FTPClient,但我猜它来自 apache commons。 FTPClient 的文档说:

The default settings for FTPClient are for it to use FTP.ASCII_FILE_TYPE , FTP.NON_PRINT_TEXT_FORMAT , FTP.STREAM_TRANSFER_MODE , and FTP.FILE_STRUCTURE . The only file types directly supported are FTP.ASCII_FILE_TYPE and FTP.BINARY_FILE_TYPE

由于 zip 文件是二进制文件,您必须添加

ftp.setFileType(FTP.BINARY_FILE_TYPE);

API:FTPClient.setFileType

关于java - 在java中使用ftp下载时文件被损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36710611/

相关文章:

java - 如何将纳秒转换为秒?

go - 尝试使用 goftp 库上传文件会出现 "502 RFC 2228 authentication not implemented."错误

Windows文件输出重定向问题

java - 线程中的异常 "main"java.sql.SQLException : Access denied for user '' @'localhost' (using password: NO)

java - 从非 EventDispatchThread 使用 JEditorPane

java - FtpStreamingMessageSource - 失败时重试

javascript - Cordova FileTransfer 不发送自定义 header

c# - UDP 文件传输 - 是的,UDP

通过 TCP 套接字的 Python 多文件传输

java - 检测失败: Resource not found