2

我在 Websphere 上有一个 java 应用程序,它使用 Apache Commons FTPClient 通过 FTP 从 Windows 服务器检索文件。当我将应用程序部署到在 Windows 环境中运行的 Websphere 时,我能够干净地检索所有文件。但是,当我在 Linux 上将相同的应用程序部署到 Webpshere 时,有时会收到不完整或损坏的文件。但是这些情况是一致的,因此相同的文件每次都会失败并返回相同数量的字节(通常只比我应该得到的少几个字节)。我想说我可以在 Linux 上成功读取大约 95% 的文件。

这是相关的代码...

ftpc = new FTPClient();
// set the timeout to 30 seconds
    ftpc.enterLocalPassiveMode();
    ftpc.setDefaultTimeout(30000);
    ftpc.setDataTimeout(30000); 
    try
    {
              String ftpServer = CoreApplication.getProperty("ftp.server");
        String ftpUserID = CoreApplication.getProperty("ftp.userid");
        String ftpPassword = CoreApplication.getProperty("ftp.password");

        log.debug("attempting to connect to ftp server = "+ftpServer);
        log.debug("credentials = "+ftpUserID+"/"+ftpPassword);

        ftpc.connect(ftpServer);

        boolean login = ftpc.login(ftpUserID, ftpPassword);

        if (login) 
        {
            log.debug("Login success...");          } 
        else 
        {
            log.error("Login failed - connecting to FTP server = "+ftpServer+", with credentials "+ftpUserID+"/"+ftpPassword);
            throw new Exception("Login failed - connecting to FTP server = "+ftpServer+", with credentials "+ftpUserID+"/"+ftpPassword);
        }

        is = ftpc.retrieveFileStream(fileName);
          ByteArrayOutputStream out = null; 
          try { 

              out = new ByteArrayOutputStream(); 
              IOUtils.copy(is, out); 
          } finally { 
              IOUtils.closeQuietly(is); 
              IOUtils.closeQuietly(out); 
          }

          byte[] bytes = out.toByteArray();
          log.info("got bytes from input stream - byte[] size is "+ bytes.length);

对此的任何帮助将不胜感激。

谢谢。

4

1 回答 1

3

我怀疑 FTP 可能使用的是 ASCII 而不是二进制传输模式,并将它认为是文件中的窗口行尾序列映射到 Unix 行尾。对于真正是文本的文件,这将起作用。对于真正的二进制文件,如果文件包含某些字节序列,结果将是损坏并且文件稍短。

FTPClient.setFileType(...)

跟进

...所以为什么这会在 Windows 而不是 Linux 上运行仍然是另一天的谜。

这个谜很容易解释。您正在将文件从 Windows 机器 FTP 传输到 Windows 机器,因此无需更改行尾标记。

于 2010-11-07T05:04:16.877 回答