Apache FTPClient - Linux 上不完整的文件检索,适用于 Windows

发布于 2024-10-01 11:57:19 字数 1809 浏览 7 评论 0原文

我在 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);

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

谢谢。

I have a java application on Websphere that is using Apache Commons FTPClient to retrieve files from a Windows server via FTP. When I deploy the application to Websphere running in a Windows environment, I am able to retrieve all of the files cleanly. However, when I deploy the same application to Webpshere on Linux, there are cases where I am getting an incomplete or corrupt files. These cases are consistent though, such that the same files will fail every time and give back the same number of bytes (usually just a few bytes less than what I should be getting). I would say that I can read approximately 95% of the files successfully on Linux.

Here's the relevant code...

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);

Any assistance with this would be greatly appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

焚却相思 2024-10-08 11:57:19

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

请参阅 FTPClient .setFileType(...)

跟进

...那么为什么这可以在 Windows 上运行而不是在 Linux 上运行,这仍然是一个谜。

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

I have a suspicion that the FTP might be using ASCII rather than binary transfer mode, and mapping what it thinks are Window end-of-line sequences in the files to Unix end-of-lines. For files that are really text, this will work. For files that are really binary, the result will be corruption and a slightly shorter file if the file contains certain sequences of bytes.

See FTPClient.setFileType(...).

FOLLOWUP

... so why this would work on Windows and not Linux remains a mystery for another day.

The mystery is easy to explain. You were FTP'ing files from a Windows machine to a Windows machine, so there was no need to change the end-of-line markers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文