使用ftp读取文件夹中的多个文件,并删除

public class FTPUtils {

    private static final Logger LOG = LoggerFactory.getLogger(FTPUtils.class);


    /**
     * 获取FTPClient对象
     *
     * @param ftpHost     FTP主机服务器
     * @param ftpPassword FTP 登录密码
     * @param ftpUserName FTP登录用户名
     * @param ftpPort     FTP端口 默认为21
     * @return
     */
    private static FTPClient getFTPClient(String ftpHost, String ftpPassword,
                                          String ftpUserName, int ftpPort) throws IOException {
        FTPClient ftpClient = null;
        ftpClient = new FTPClient();
        ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
        ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
        if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
            LOG.info("未连接到FTP,用户名或密码错误。");
            ftpClient.disconnect();
        } else {
            LOG.info("FTP连接成功。");
        }
        return ftpClient;
    }

    /**
     * 去 服务器的FTP路径下上读取文件
     *
     * @param ftpPath
     * @param
     * @return
     */
    public static List<JSONArray> readConfigFileForFTP(String ftpPath, String ftpHost, String ftpPassword,
                                                       String ftpUserName, int ftpPort) {
        InputStream in;
        FTPClient ftpClient = null;
        try {
            ftpClient = getFTPClient(ftpHost, ftpPassword, ftpUserName, ftpPort);
            List<JSONArray> list = new ArrayList<>();
            LOG.info("开始读取绝对路径" + ftpPath + "文件!");
            try {
                ftpClient.setControlEncoding("UTF-8"); // 中文支持
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                ftpClient.enterLocalPassiveMode();
                ftpClient.changeWorkingDirectory(ftpPath);
                FTPFile[] ftpFiles = ftpClient.listFiles(ftpPath);
                for (FTPFile file : ftpFiles) {
                    String name = file.getName();
                    in = ftpClient.retrieveFileStream(new String(name.getBytes("UTF-8"), "ISO-8859-1"));
                    if (in != null) {
                        StringBuffer resultBuffer = new StringBuffer();
                        BufferedReader br = new BufferedReader(new InputStreamReader(in));
                        String data;
                        try {
                            while ((data = br.readLine()) != null) {
                                resultBuffer.append(data);
                            }
                            String string = resultBuffer.toString();
                            JSONArray jsonArray = JSONArray.parseArray(string);
                            list.add(jsonArray);
                            in.close();
                            boolean flag = ftpClient.deleteFile(name);
                            if (flag) {
                                System.out.println("删除文件成功");
                            }
                            ftpClient.completePendingCommand();
                        } catch (IOException e) {
                            LOG.error("文件读取错误。");
                            e.printStackTrace();
                        }
                    } else {
                        LOG.error("in为空,不能读取。");
                    }
                }
            } catch (FileNotFoundException e) {
                LOG.error("没有找到" + ftpPath + "文件");
                e.printStackTrace();
            } catch (SocketException e) {
                LOG.error("连接FTP失败.");
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
                LOG.error("文件读取错误。");
            }
            return list;
        } catch (SocketException e) {
            LOG.info("FTP的IP地址可能错误,请正确配置。");
        } catch (IOException e) {
            LOG.info("FTP的端口错误,请正确配置。");
        }
        return null;
    }

    /**
     * * 删除文件 *
     *
     * @param pathname FTP服务器保存目录 *
     * @param filename 要删除的文件名称 *
     * @return
     */
    private static void deleteFile(String pathname, String filename, FTPClient ftpClient) {
        try {
            System.out.println("开始删除文件");
            // 切换FTP目录
            ftpClient.changeWorkingDirectory(pathname);
            boolean flag = ftpClient.deleteFile(filename);
            ftpClient.logout();
            if (flag) {
                System.out.println("删除文件成功");
            }
        } catch (Exception e) {
            System.out.println("删除文件失败");
            e.printStackTrace();
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

适用于读取一个文件夹中的多个文件,并且每读取一个删除一个。

这里有一个小坑,就是如果文件夹里面有多个文件的时候,读取下一个文件的时候,输入流会报空指针异常,这里关键的一步就是

  ftpClient.completePendingCommand();
必须加上这行代码。另外输出文件一定要先关闭流的使用!

转载于:https://www.cnblogs.com/wxw7blog/p/9929092.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值