Apache FTPClient - Linux上的不完整文件检索,适用于Windows
问题描述:
我在Websphere上有一个Java应用程序,它使用Apache Commons FTPClient通过FTP从Windows服务器检索文件。当我将应用程序部署到在Windows环境中运行的Websphere时,我可以干净地检索所有文件。但是,当我将相同的应用程序部署到Linux上的Webpshere时,有时会收到不完整或损坏的文件。这些情况是一致的,因此相同的文件每次都会失败并返回相同数量的字节(通常只比我应该得到的少几个字节)。我会说我可以在Linux上成功读取大约95%的文件。Apache FTPClient - Linux上的不完整文件检索,适用于Windows
下面是相关的代码...
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);
这个任何援助将不胜感激。
谢谢。
答
我怀疑FTP可能使用ASCII而不是二进制传输模式,并且将它认为是文件中的窗口结束行序列映射到Unix行尾。对于真正的文本文件,这将起作用。对于真正为二进制文件的文件,如果文件包含特定的字节序列,则结果将会损坏并且文件会稍微变短。
请参阅FTPClient.setFileType(...)
。
随访
...那么,为什么这会在Windows上工作,而不是Linux仍然是一天一个谜。
这个谜很容易解释。您正在将Windows计算机上的文件传输到Windows计算机,因此无需更改行尾标记。
Stephen,你说的没错。我修改了我的代码,以便在必要时使用FTP.BINARY_FILE_TYPE的值来执行setFileType()。现在我正在使我的文件保持原样。文档建议ASCII是默认设置,为什么这可以在Windows上运行,而不是Linux仍然存在一个神秘的另一天。 – weskelton 2010-11-10 14:14:05