我们有一个客户端 FTP 服务器,他们在 /dev 目录中上传多个 pdf 文件。pdf 文件的格式为 [ORGANAIZATION_NAME]_CR.pdf 。我们正在检索这些文件并将其上传到基于 [ORGANAIZATION_NAME] 的 Liferay 文档库。
我们在使用单个 ftp 连接检索多个文件时遇到问题。第一次检索当前目录(/dev)的文件列表并上传到 Liferay 文档库。但是当它试图检索第二个文件时,它返回文件的长度为零并抛出错误作为异常发送警报:java.net.SocketException:对等方重置连接:套接字写入错误:连接关闭而没有指示。
请参考以下代码。
public static void importReports() {
InputStream is = null;
System.setProperty("javax.net.debug", "ssl");
FTPSClient ftpClient = new FTPSClient();
try
{
// Store file on host
ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
// Connect to host
ftpClient.connect(hostname);
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
System.exit(1);
}
if (!ftpClient.login(loginname), PortletProps.get(password))) {
ftpClient.logout();
System.exit(1);
}
// Set protection buffer size
ftpClient.execPBSZ(0);
// Set data channel protection to private
ftpClient.execPROT("P");
try{
List<Organization> orgList = OrganizationLocalServiceUtil.getOrganizations(-1, -1);
for (Organization organization : orgList) {
String folderPath ="/dev";
String ftpfileName = organization.getName();
String ftpFilePath = folderPath+StringPool.FORWARD_SLASH+ftpfileName;
ftpClient.changeWorkingDirectory(folderPath);
// Enter local passive mode
ftpClient.enterLocalPassiveMode();
ftpClient.setRemoteVerificationEnabled(false);
FTPFile[] files = ftpClient.listFiles(folderPath);
for(FTPFile file : files)
{
if(file.isFile() && file.getSize() > 0 && file.getName().equalsIgnoreCase(ftpfileName))
{
try{
is = ftpClient.retrieveFileStream(ftpFilePath);
if (Validator.isNotNull(is)) {
ftpClient.completePendingCommand();
}catch(SocketException se)
{
LOGGER.info("SocketException :"+se.getMessage());
}
}
}
}
// Logout
ftpClient.logout();
// Disconnect
ftpClient.disconnect();
}
catch(PortalException e)
{
LOGGER.info(e.getMessage());
} catch (SystemException e) {
LOGGER.info(e.getMessage());
}
} catch (IOException ioe) {
LOGGER.info(ioe.getMessage());
}
finally
{
try {
// Logout
ftpClient.logout();
// Disconnect
ftpClient.disconnect();
} catch (IOException e) {
LOGGER.info(e.getMessage());
}
}
}