在Java中使用ftp下载时文件已损坏 [英] Files are getting corrupted when download using ftp in java

查看:227
本文介绍了在Java中使用ftp下载时文件已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试提取压缩文件时,文件已损坏.这是我使用的代码.我无法理解我要怎么做.文件的格式为.zip,其中包含XML文件.从远程服务器下载后,XML文件标签被更改并且看上去已损坏.

Files are getting corrupted when I am trying to pull a compressed files. Here is the code I have used. I am not able to understand what I am going wrong. The format of files is .zip and inside it has XML files. After downloading it from remote server,XML files tags are changed and looks corrupted.

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 的文档说:

You didn't say which FTPClient you use, but I'm guessing it is from apache commons. Documentation for FTPClient says:

FTPClient的默认设置是使用FTP.ASCII_FILE_TYPE,FTP.NON_PRINT_TEXT_FORMAT,FTP.STREAM_TRANSFER_MODE和FTP.FILE_STRUCTURE.直接支持的唯一文件类型是FTP.ASCII_FILE_TYPE和FTP.BINARY_FILE_TYPE

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文件是二进制文件,因此您必须添加

As the zip-file is a binary file you have to add

ftp.setFileType(FTP.BINARY_FILE_TYPE);

API: FTPClient.setFileType

这篇关于在Java中使用ftp下载时文件已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆