将多个文件从ftps服务器传输到Liferay Portal

时间:2013-05-03 10:43:26

标签: ftp liferay sftp ftp-client

我们有一个客户端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());
        }
    }
}

0 个答案:

没有答案