FTP判断文件是否存在

参考, 注意最后的评论

FTP Client使用的是Apache Commons Net 3.3


    /**
     * 检查FTP上指定文件是否存在
     * @param remoteFilePartNameList 文件路径
     * @throws BusinessException
     * @throws IOException
     */
    private void checkFtpFileExist(List<String> remoteFilePartNameList) throws BusinessException, IOException {
        FTPClient ftp = new FTPClient();
        String serverIP = "1.1.1.1";
        String serverUserName = "user";
        String serverPassword = "pwd";
        try {
            ftp.connect(serverIP);
            ftp.login(serverUserName, serverPassword);

            if (FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                ftp.enterLocalPassiveMode();
                ftp.setFileType(FTP.BINARY_FILE_TYPE);
                StringBuilder sb = null;
                
                for (String remoteFileName : remoteFilePartNameList) {
                    InputStream inputStream = ftp.retrieveFileStream(remoteFileName);
                    if (inputStream == null || ftp.getReplyCode() == 550) {
                        // 文件不存在 
                        if (sb == null) {
                            sb = new StringBuilder();
                        }
                        String[] strings = StringUtils.split(remoteFileName, Constants.SLASH);
                        sb.append(strings[strings.length - 1]).append(", ");
                    }
                    
                    if (inputStream != null) {
                        inputStream.close();
                        ftp.completePendingCommand(); // 必须执行,否则在循环检查多个文件时会出错
                    }
                }
                
                if (sb != null && sb.length() > 2) {
                    sb.setLength(sb.length() - 2);
                    String notfoundReportPaymentIdStr = sb.toString();
                    log.error("FTP SERVER上未找到以下文件[" + notfoundReportPaymentIdStr + "]");
                    String exceptionMessage = i18nServiceImpl.queryValue("E374", "文件{0}不存在!");
                    throw new BusinessException(StringUtils.replace(exceptionMessage, "{0}", notfoundReportPaymentIdStr));
                }
            }
            else {
                log.error("连接FTP SERVER失败,SERVER IP[" + serverIP + "], USER NAME[" + serverUserName + "], Ftp response:" + ftp.getReplyString());
                ftp.disconnect();
                throw new BusinessException(i18nServiceImpl.queryValue("E373", "连接FTP失败!"));
            }
            ftp.logout();
        }
        catch (IOException e) {
            log.error("连接FTP SERVER失败,SERVER IP[" + serverIP + "], USER NAME[" + serverUserName + "]", e);
            throw e;
        }
        finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                }
                catch (IOException ioe) {
                    // do nothing
                }
            }
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,403评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,054评论 6 342
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,386评论 25 709
  • spring官方文档:http://docs.spring.io/spring/docs/current/spri...
    牛马风情阅读 5,694评论 0 3
  • 去年此时,我也曾在这样的离别时刻写过一封没有送出的信,我想原因大概是你比较忙吧。 看山看水看风雨,缘起缘灭缘无尽。...
    瓶邪胖阅读 1,586评论 0 0