现需求,读取FTP上的JSON文件,操作完成后把源文件删除并且创建备份文件夹被封。
在网上找了好多例子都用不了。自己尝试着取写了一个。
经测试可用。
public static FTPClient getFTPClient(String ftpHost, String ftpUserName,
String ftpPassword, int ftpPort) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient = new FTPClient();
ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
ftpClient.disconnect();
logger.error("未连接到FTP,用户名或密码错误。");
} else {
logger.info("FTP connection success");
}
} catch (SocketException e) {
e.printStackTrace();
logger.error("FTP的IP地址可能错误,请正确配置。");
} catch (IOException e) {
logger.error("FTP的IP地址可能错误,请正确配置。");
}
return ftpClient;
}
public static List<String> downloadFtpFile(String ftpHost, String ftpUserName,
String ftpPassword, int ftpPort, String ftpPath) throws IOException {
//所读出来的JSON全部放在List里,最终循环LIST去解析操作
List<String> list = new ArrayList<String>();
FTPClient ftpClient = null;
/*BufferedReader read = null;*/
try {
ftpClient = getFTPClient(ftpHost, ftpUserName, ftpPassword, ftpPort);
ftpClient.setControlEncoding("UTF-8"); // 中文支持
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); // 使用二进制保存方式
ftpClient.changeWorkingDirectory(ftpPath);
String remoteAbsoluteFile = "";
// 列出该目录下所有文件
FTPFile[] fs = ftpClient.listFiles();
//遍历循环该文件夹下的文件
for (FTPFile ftpFile : fs) {
//进入文件夹路径
ftpClient.changeWorkingDirectory(ftpPath);
//获取文件名称
remoteAbsoluteFile = ftpFile.getName();
//获取远程文件的输入流
InputStream ins = ftpClient.retrieveFileStream(new String(remoteAbsoluteFile.getBytes("UTF-8"), "iso-8859-1"));
BufferedReader read = new BufferedReader(new InputStreamReader(ins));
String lineTxt = null;
while ((lineTxt = read.readLine()) != null) {
list.add(lineTxt);
//System.out.println(lineTxt);
}
//这步很重要,如果不加,第二次进来后,InputStream就会为空
ftpClient.completePendingCommand();
//每读取完一次文件,都要关闭读的操作
ins.close();
read.close();
}
uploadFile(ftpClient,ftpPath,ftpPath+"log", fs);
}catch (FileNotFoundException e) {
//logger.error("没有找到文件");
throw new FileNotFoundException("没有找到文件");
} catch (SocketException e) {
logger.error("连接FTP失败.");
throw new SocketException("连接FTP失败");
} catch (IOException e) {
logger.error("文件读取错误。");
throw new IOException("文件读取错误");
}
return list;
}
public static void uploadFile(FTPClient ftpClient,String path,String ftpPath, FTPFile[] fs) throws IOException {
//循环之前问价夹下面的文件数目,是为了删除之前文件夹里的所有文件
for (FTPFile ftpFile : fs) {
//跳转到套删除文件的文件夹
ftpClient.changeWorkingDirectory(path);
//当前目录
System.out.println(ftpClient.printWorkingDirectory());
//遍历循环该文件夹下的文件
String remoteAbsoluteFile = ftpFile.getName();
//获取远程文件的输入流
InputStream ins = ftpClient.retrieveFileStream(remoteAbsoluteFile);
//删除文件夹下面的文件
ftpClient.deleteFile(remoteAbsoluteFile);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
String date = dateFormat.format(new Date());
//当前问价夹名称+log当备份文件夹名称,第二层文件夹是时间。
String name = ftpPath + "/" + date;
//根据斜杠取拆分
StringTokenizer s = new StringTokenizer(name, "/");
s.countTokens();
String pathName = "";
while (s.hasMoreElements()) {
pathName = pathName + "/" + (String) s.nextElement();
try {
ftpClient.mkd(pathName);
} catch (Exception e) {
logger.info("创建失败");
}
}
//这步代表操作结束
ftpClient.completePendingCommand();
//进入指定路径
ftpClient.changeWorkingDirectory(pathName);
//上传文件
boolean r = ftpClient.storeFile(remoteAbsoluteFile, ins);
//为什么调用两次,因为不在同一级别目录下面 ../../是代表返回根目录,有几层就返回几层。虽然有点傻
ftpClient.changeWorkingDirectory("../../");
/*ftpClient.changeToParentDirectory();
ftpClient.changeToParentDirectory();*/
}
ftpClient.logout();
}