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

时间:2023-04-09 19:06:32
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();
必须加上这行代码。另外输出文件一定要先关闭流的使用!