上篇(https://blog.csdn.net/oLengYueHun/article/details/108462454)我们说到,对于java.io.File的增量同步,其基本步骤如下:
1.获取两个文件夹总目录和该目录下所有文件和文件夹的映射(Map<String,File>),得到映射A和映射B
2.比较两个映射间A有而B没有的映射添加到映射C中去
3.根据映射C指向的文件将其从A对应的目录中同步到B对应的目录中去
对于org.apache.commons.net.ftp.FTPClient显然不能用上述方式,因为FTPFile和java.io.File无法直接比较,所以我选择先将其转化为Map<String,String> ,如果两边都用Map<String,String>又会产生另一个问题:路径的左斜和右斜会不断搞出奇奇怪怪的IOException和NullPointerException,所以我根据Map<String,String>在本地某目录下建立0kb的文件和文件夹,再与Map<String,File>比较,将得到的结果重新转化为Map<String,String>进行ftp操作。以下是详细代码
package com.gwhn.util;
import com.gwhn.controller.TaskController;
import com.gwhn.entity.FTPMessage;
import com.gwhn.service.impl.FTPServiceImpl;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
import java.io.*;
import java.net.SocketException;
import java.net.URL;
import java.util.*;
/**
* @author banxian
* @date 2020/10/23 10:55
*/
public class FTPUtil {
private static Logger logger = Logger.getLogger(FTPServiceImpl.class);
/**
* 获取FTPClient对象
*
* @param ftpMessage ftp服务信息
* @return FTPClient
*/
public FTPClient getFTPClient(FTPMessage ftpMessage) {
FTPClient ftp = null;
try {
ftp = new FTPClient();
ftp.connect(ftpMessage.getFtpHost(), ftpMessage.getFtpPort()); // 连接FPT服务器,设置IP及端口
ftp.login(ftpMessage.getFtpUserName(), ftpMessage.getFtpPassword());// 设置用户名和密码
ftp.setConnectTimeout(50000); // 设置连接超时时间,50000毫秒
ftp.setControlEncoding("UTF-8"); // 设置中文编码集,防止中文乱码
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
logger.info("未连接到FTP,用户名或密码错误");
ftp.disconnect();
} else {
logger.info("ftp连接到" + ftpMessage.getFtpHost() + "成功");
}
} catch (SocketException e) {
e.printStackTrace();
logger.info("FTP的IP地址可能错误,请正确配置");
} catch (IOException e) {
e.printStackTrace();
logger.info("FTP的端口错误,请正确配置");
}
return ftp;
}
/**
* 获取目录下所有文件/文件夹名
*
* @param ftpClient
* @return List<String>
* @author banxian
* @date: 2020/10/23 10:57
*/
public int getFtpFilesNum(FTPClient ftpClient, String path) {
logger.info("开始获取" + ftpClient.getPassiveLocalIPAddress() + "___" + path + "文件数量");
int m = 0;
try {
ftpClient.changeWorkingDirectory(path);
FTPFile[] files = ftpClient.listFiles();
for (int i = 0; i < files.length; i++) {
if (!files[i].isDirectory()) {
m++;
} else {
m += getFtpFilesNum(ftpClient, path + "/" + files[i].getName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return m;
}
/**
* 关闭FTP方法
*
* @param ftp
* @return
*/
public boolean closeFTP(FTPClient ftp) {
try {
ftp.logout();
} catch (Exception e) {
logger.error("FTP关闭失败");
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
logger.error("FTP关闭失败");
}
}
}
return false;
}
/**
* 下载FTP下指定文件
*
* @param ftp FTPClient对象
* @param remotePath FTP要下载的文件的父文件夹路径
* @param fileName FTP要下载的文件的文件名
* @param downPath 下载保存的目录
* @return
*/
public void downLoadFTP(FTPClient ftp, String remotePath, String fileName,
String downPath) {
// 默认失败
boolean flag = false;
remotePath = remotePath.replaceAll("\\\\", "/");
try {
ftp.changeWorkingDirectory(remotePath);
ftp.enterLocalPassiveMode();// 获取目录下文件集合
File downFile = new File(downPath + File.separator
+ fileName); // 取得指定文件并下载
OutputStream out = new FileOutputStream(downFile);
// 绑定输出流下载文件,需要设置编码集,不然可能出现文件为空的情况
flag = ftp.retrieveFile(new String(fileName.getBytes("UTF-8"), "ISO-8859-1"), out);
out.flush();
out.close();
if (flag) {
logger.info("下载ftp___" + remotePath + File.separator
+ fileName + "到本机___" + downPath + "\\" + fileName + "成功");
} else {
logger.error("下载ftp___" + remotePath + File.separator
+ fileName + "到本机___" + downPath + "\\" + fileName + "失败");
}
} catch (Exception e) {
logger.error("下载失败");
}
}
/**
* 全量ftp下载三天内新建或有修改的文件夹
*
* @param ftpClient ftp
* @param localPath 本地路径
* @param remotePath 远程路径
* @return
* @author banxian
* @date: 2020/11/11 16:50
*/
public void downLoadFTPFiles(FTPClient ftpClient, String localPath, String remotePath) {
try {
String fileName = new File(remotePath).getName();//远程文件夹名
localPath = localPath + "\\" + fileName;//本地文件夹路径
new File(localPath).mkdirs();//新建本地文件夹
ftpClient.changeWorkingDirectory(fileName);
FTPFile[] ftpFiles = ftpClient.listFiles();
for (int i = 0; i < ftpFiles.length; i++) {
if (!ftpFiles[i].isDirectory()) {
// System.out.println(remotePath+"\\"+ftpFiles[i].getName());
long now = System.currentTimeMillis();
Date date = ftpFiles[i].getTimestamp().getTime();
//System.out.println(date.toString());
long a = (now - date.getTime()) / (3600000);//当前时间-文件最后修改时间(小时)
if (a <= 72) {//如果a小于三天,则从ftp端下载到本地
downLoadFTP(ftpClient, remotePath, ftpFiles[i].getName(), localPath);
}
} else {
downLoadFTPFiles(ftpClient, localPath, remotePath + "\\" + ftpFiles[i].getName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据映射下载ftp文件夹
*
* @param localFilePath
* @param ftpClient
* @param ftpSrcFilePath
* @param unCopyFilesMap
* @return
* @author banxian
* @date: 2021/1/12 15:51
*/
public void downLoadFTPFilesByMap(FTPClient ftpClient, String ftpSrcFilePath, String localFilePath, Map<String, String> unCopyFilesMap) {
for (String filePath : unCopyFilesMap.keySet()) {
String fileName = unCopyFilesMap.get(filePath);
String relativePath = filePath.substring(ftpSrcFilePath.length(), filePath.indexOf(fileName) - 1);
try {
File parentFile = new File(localFilePath + "/" + relativePath);
if (!parentFile.exists()) {
parentFile.mkdirs();
}
String ftpFilePath = ftpSrcFilePath + relativePath;
downLoadFTP(ftpClient, ftpSrcFilePath + relativePath, fileName, parentFile.getPath());
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 增量下载ftp文件夹
*
* @param ftpClient
* @param srcPath
* @param desPath
* @return
* @author banxian
* @date: 2021/1/12 15:51
*/
public void downloadFtpFilesBySrcPathDesPath(FTPClient ftpClient, String srcPath, String desPath) {
//先清理缓存文件夹
URL url = TaskController.class.getClassLoader().getResource("application.yml");
java.io.File projectFile = new java.io.File(url.getFile());
File tempFile = new File(projectFile.getParent() + "/temp" + srcPath);
FileUtil fileUtil = new FileUtil();
fileUtil.deleteDirectoryByMinute(tempFile, 0);
desPath = desPath.replaceAll("\\\\", "/");//ftp只识别/路径,不识别\\路径,简单处理下
FileMapUtil fileMapUtil = new FileMapUtil();
Map<String, String> fileMap1 = new HashMap<String, String>();
Map<String, File> fileMap2 = new HashMap<String, File>();
fileMap1 = getFtpFileMap(ftpClient, srcPath, srcPath, fileMap1);//获取ftp文件夹的映射,filePath--->fileName
fileMap2 = fileMapUtil.getFileMap(new File(desPath), fileMap2);//获取本地文件夹的映射,filePath--->java.io.File
Map<String, File> fileMap = fileMapUtil.transFtpFileToFileMap(fileMap1);//将file--->fileName通过在本地缓存文件夹新建文件的形式转换为filePath--->File
Map<String, File> unCopyFileMap = fileMapUtil.getUnCopyFileMap(fileMap, new File(projectFile.getParent() + "/temp" + srcPath), fileMap2, new File(desPath));//比较两个映射获取未同步的文件映射
Map<String, String> unCopyFtpFileMap = fileMapUtil.transFileToFtpFileMap(projectFile.getParent() + "/temp", unCopyFileMap);//filePath--->java.io.File转换为filePath--->fileName以方便调用ftp下载方法
downLoadFTPFilesByMap(ftpClient, srcPath, desPath, unCopyFtpFileMap);
}
/**
* FTP文件上传
*
* @param ftp
* @param filePath
* @param ftpPath
* @return
*/
public boolean uploadFile(FTPClient ftp, String filePath, String ftpPath) {
boolean flag = false;
InputStream in = null;
try {
ftp.enterLocalPassiveMode();// 设置PassiveMode传输
ftp.setFileType(FTPClient.BINARY_FILE_TYPE); //设置二进制传输,使用BINARY_FILE_TYPE,ASC容易造成文件损坏
if (!ftp.changeWorkingDirectory(ftpPath)) { //FPT目标文件夹不存在则创建
ftp.makeDirectory(ftpPath);
}
ftp.changeWorkingDirectory(ftpPath);
//上传文件
File file = new File(filePath);
in = new FileInputStream(file);
String tempName = ftpPath + "/" + file.getName();
try {
flag = ftp.storeFile(new String(tempName.getBytes("UTF-8"), "ISO-8859-1"), in);
} catch (Exception e) {
e.printStackTrace();
}
if (flag) {
logger.info("上传" + filePath + File.separator
+ "到ftp___" + tempName + "成功");
} else {
logger.error("上传" + filePath + File.separator
+ "到ftp___" + tempName + "失败");
}
} catch (Exception e) {
e.printStackTrace();
logger.error("上传失败");
} finally {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}
/**
* 全量上传文件夹
*
* @param ftpClient
* @param localPath
* @param remotePath
* @author banxian
* @date: 2020/11/18 17:47
*/
public void uploadFiles(FTPClient ftpClient, String localPath, String remotePath) {
try {
File srcFile = new File(localPath);
if (!srcFile.exists()) {
srcFile.mkdirs();
}
File[] files = srcFile.listFiles();
for (int i = 0; i < files.length; i++) {
if (!files[i].isDirectory()) {
uploadFile(ftpClient, files[i].getPath(), remotePath);
} else {
if (!ftpClient.changeWorkingDirectory(remotePath + "/" + files[i].getName())) { //FPT目标文件夹不存在则创建
ftpClient.makeDirectory(remotePath + "/" + files[i].getName());
logger.info("创建目录:ftp___" + remotePath + "/" + files[i].getName());
}
ftpClient.changeWorkingDirectory(remotePath + "/" + files[i].getName());//转换工作目录
logger.info("\n\n转换到工作目录:ftp___" + remotePath + "/" + files[i].getName());
uploadFiles(ftpClient, localPath + "/" + files[i].getName(), remotePath + "/" + files[i].getName());//迭代循环
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据映射上传ftp文件
*
* @param ftpClient
* @param srcPath
* @param remotePath
* @param fileMap
* @return
* @author banxian
* @date: 2021/1/13 9:38
*/
public void uploadFilesByMap(FTPClient ftpClient, String srcPath, String remotePath, Map<String, String> fileMap) {
for (String filePath : fileMap.keySet()) {
String fileName = fileMap.get(filePath);
String relativePath = filePath.substring(0, filePath.indexOf(fileName) - 1).replaceAll("\\\\", "/");
try {
List<String> relativePathList = new ArrayList<>();
//FPT目标文件夹不存在则一级一级创建
if (!ftpClient.changeWorkingDirectory(remotePath + relativePath)) {
for (int i = 0; i < relativePath.length(); i++) {
if (relativePath.substring(i, i + 1).equals("/") || relativePath.substring(i, i + 1) == "/") {
relativePathList.add(relativePath.substring(0, i));
ftpClient.makeDirectory(remotePath + relativePath.substring(0, i));
}
}
}
ftpClient.changeWorkingDirectory(remotePath + relativePath);//转换工作目录
uploadFile(ftpClient, srcPath + filePath, remotePath + relativePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 根据源路径和目标路径处理增量上传,要用到根据映射上传
* @param ftpClient
* @param srcPath
* @param desPath
* @return
* @author banxian
* @date: 2021/1/13 16:15
*/
public void uploadFilesByPath(FTPClient ftpClient, String srcPath, String desPath) {
FileMapUtil fileMapUtil = new FileMapUtil();
desPath = desPath.replaceAll("\\\\", "/");//ftp只识别/路径,不识别\\路径
URL url = TaskController.class.getClassLoader().getResource("application.yml");
java.io.File projectFile = new java.io.File(url.getFile());
File tempFile = new File(projectFile.getParent() + "/temp" + desPath);
FileUtil fileUtil = new FileUtil();
fileUtil.deleteDirectoryByMinute(tempFile, 0);
Map<String, File> srcFileMap = new HashMap<String, File>();
Map<String, String> desFileMap = new HashMap<>();
srcFileMap = fileMapUtil.getFileMap(new File(srcPath), srcFileMap);
desFileMap = getFtpFileMap(ftpClient, srcPath, desPath, desFileMap);
Map<String, File> desFileMap1 = fileMapUtil.transFtpFileToFileMap(desFileMap);
Map<String, File> unCopyFtpMap = fileMapUtil.getUnCopyFileMap(srcFileMap, new File(srcPath), desFileMap1, new File(projectFile.getParent() + "/temp" + desPath));
Map<String, String> unCopyFileMap = fileMapUtil.transFileToFtpFileMap(srcPath, unCopyFtpMap);
uploadFilesByMap(ftpClient, srcPath, desPath, unCopyFileMap);
}
/**
* FPT上文件的复制
*
* @param ftp FTPClient对象
* @param olePath 原文件地址
* @param newPath 新保存地址
* @param fileName 文件名
* @return
*/
public boolean copyFile(FTPClient ftp, String olePath, String newPath, String fileName) {
boolean flag = false;
try {
// 跳转到文件目录
ftp.changeWorkingDirectory(olePath);
//设置连接模式,不设置会获取为空
ftp.enterLocalPassiveMode();
// 获取目录下文件集合
FTPFile[] files = ftp.listFiles();
ByteArrayInputStream in = null;
ByteArrayOutputStream out = null;
for (FTPFile file : files) {
// 取得指定文件并下载
if (file.getName().equals(fileName)) {
//读取文件,使用下载文件的方法把文件写入内存,绑定到out流上
out = new ByteArrayOutputStream();
ftp.retrieveFile(new String(file.getName().getBytes("UTF-8"), "ISO-8859-1"), out);
in = new ByteArrayInputStream(out.toByteArray());
//创建新目录
ftp.makeDirectory(newPath);
//文件复制,先读,再写
//二进制
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
flag = ftp.storeFile(newPath + File.separator + (new String(file.getName().getBytes("UTF-8"), "ISO-8859-1")), in);
out.flush();
out.close();
in.close();
if (flag) {
logger.info("转存成功");
} else {
logger.error("复制失败");
}
}
}
} catch (Exception e) {
logger.error("复制失败");
}
return flag;
}
/**
* 实现文件的移动,这里做的是一个文件夹下的所有内容移动到新的文件,
* 如果要做指定文件移动,加个判断判断文件名
* 如果不需要移动,只是需要文件重命名,可以使用ftp.rename(oleName,newName)
*
* @param ftp
* @param oldPath
* @param newPath
* @return
*/
public boolean moveFile(FTPClient ftp, String oldPath, String newPath) {
boolean flag = false;
try {
ftp.changeWorkingDirectory(oldPath);
ftp.enterLocalPassiveMode();
//获取文件数组
FTPFile[] files = ftp.listFiles();
//新文件夹不存在则创建
if (!ftp.changeWorkingDirectory(newPath)) {
ftp.makeDirectory(newPath);
}
//回到原有工作目录
ftp.changeWorkingDirectory(oldPath);
for (FTPFile file : files) {
//转存目录
flag = ftp.rename(new String(file.getName().getBytes("UTF-8"), "ISO-8859-1"), newPath + File.separator + new String(file.getName().getBytes("UTF-8"), "ISO-8859-1"));
if (flag) {
logger.info(file.getName() + "移动成功");
} else {
logger.error(file.getName() + "移动失败");
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error("移动文件失败");
}
return flag;
}
/**
* 删除FTP上指定文件夹下文件及其子文件方法,添加了对中文目录的支持
*
* @param ftp FTPClient对象
* @param FtpFolder 需要删除的文件夹
* @return
*/
public boolean deleteByFolder(FTPClient ftp, String FtpFolder) {
boolean flag = false;
try {
ftp.changeWorkingDirectory(new String(FtpFolder.getBytes("UTF-8"), "ISO-8859-1"));
ftp.enterLocalPassiveMode();
FTPFile[] files = ftp.listFiles();
for (FTPFile file : files) {
//判断为文件则删除
if (file.isFile()) {
ftp.deleteFile(new String(file.getName().getBytes("UTF-8"), "ISO-8859-1"));
}
//判断是文件夹
if (file.isDirectory()) {
String childPath = FtpFolder + File.separator + file.getName();
//递归删除子文件夹
deleteByFolder(ftp, childPath);
}
}
//循环完成后删除文件夹
flag = ftp.removeDirectory(new String(FtpFolder.getBytes("UTF-8"), "ISO-8859-1"));
if (flag) {
logger.info(FtpFolder + "文件夹删除成功");
} else {
logger.error(FtpFolder + "文件夹删除成功");
}
} catch (Exception e) {
e.printStackTrace();
logger.error("删除失败");
}
return flag;
}
/**
* 遍历解析文件夹下所有文件
*
* @param folderPath 需要解析的的文件夹
* @param ftp FTPClient对象
* @return
*/
public boolean readFileByFolder(FTPClient ftp, String folderPath) {
boolean flage = false;
try {
ftp.changeWorkingDirectory(new String(folderPath.getBytes("UTF-8"), "ISO-8859-1"));
//设置FTP连接模式
ftp.enterLocalPassiveMode();
//获取指定目录下文件文件对象集合
FTPFile files[] = ftp.listFiles();
InputStream in = null;
BufferedReader reader = null;
for (FTPFile file : files) {
//判断为txt文件则解析
if (file.isFile()) {
String fileName = file.getName();
if (fileName.endsWith(".txt")) {
in = ftp.retrieveFileStream(new String(file.getName().getBytes("UTF-8"), "ISO-8859-1"));
reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String temp;
StringBuffer buffer = new StringBuffer();
while ((temp = reader.readLine()) != null) {
buffer.append(temp);
}
if (reader != null) {
reader.close();
}
if (in != null) {
in.close();
}
//ftp.retrieveFileStream使用了流,需要释放一下,不然会返回空指针
ftp.completePendingCommand();
//这里就把一个txt文件完整解析成了个字符串,就可以调用实际需要操作的方法
System.out.println(buffer.toString());
}
}
//判断为文件夹,递归
if (file.isDirectory()) {
String path = folderPath + File.separator + file.getName();
readFileByFolder(ftp, path);
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error("文件解析失败");
}
return flage;
}
/**
* 若有ftp文件夹目录如下:
* ftp: gz/src:/src/dd1,dd2:dd1/ddd1,ddd2:
* ddd1/gz1.txt,gz.txt
* 要获取/gz/src下所有文件路径及文件映射
* 其中,sourcePath为/gz/src,filePath为每个文件或文件夹的路径
* 如果filePath对应的ftpfile是文件夹,则切换到该文件夹工作目录;
* 如果不是,则将filePath和对应的sourcePath为录入映射
* 录入的映射格式为:\gz\src/dd1/ddd1/gz1.txt -> \gz\src
*
* @param ftpClient
* @param sourcePath
* @param filePath
* @param ftpFileMap
* @return Map<String, String>
* @author banxian
* @date: 2021/1/6 10:55
*/
public Map<String, String> getFtpFileMap(FTPClient ftpClient, String sourcePath, String filePath, Map<String, String> ftpFileMap) {
try {
// ftpClient.setControlEncoding("GBK");
ftpClient.changeWorkingDirectory(filePath);
FTPFile[] ftpFiles = ftpClient.listFiles();
for (FTPFile f : ftpFiles) {
if (f.isDirectory()) {
getFtpFileMap(ftpClient, sourcePath, filePath + "/" + f.getName(), ftpFileMap);
} else {
long now = System.currentTimeMillis();
Date date = f.getTimestamp().getTime();
//System.out.println(date.toString());
long a = (now - date.getTime()) / (3600000);//当前时间-文件最后修改时间(小时)
if (a <= 72) {//如果a小于三天,则加载到映射中
ftpFileMap.put(filePath + "/" + f.getName(), f.getName());
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
return ftpFileMap;
}
}
}
package com.gwhn.util;
import com.gwhn.controller.TaskController;
import com.gwhn.service.impl.FTPServiceImpl;
import org.apache.log4j.Logger;
import java.io.File;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* @author banxian
* @date 2020/11/24 15:56
*/
public class FileMapUtil {
private static Logger logger = Logger.getLogger(FileMapUtil.class);
/**
* 获取文件映射
* @param file
* @param fileMap
* @return
* @author banxian
* @date: 2021/1/13 16:31
*/
public Map<String, File> getFileMap(File file, Map<String, File> fileMap) {
if (!file.exists()) {
file.mkdirs();
}
Map<String, File> fileMap1 = new HashMap<String, File>();
if (fileMap != null) {
for (String filePath : fileMap.keySet()) {
fileMap1.put(filePath, fileMap.get(filePath));
}
}
Long now = Long.valueOf((new Date()).getTime());
for (File f : file.listFiles()) {
if (!f.isDirectory()) {
long milliseconds = f.lastModified();
if((now.longValue() - milliseconds) / 3600000 < 72) {//三天内的文件加载到映射中
fileMap1.put(f.getPath(), f);
}
} else {
Map<String, File> fileMap2 = getFileMap(f, fileMap1);
if (fileMap2 != null && fileMap2.size() > 0) {
for (String filePath : fileMap2.keySet()) {
long milliseconds = fileMap2.get(filePath).lastModified();
if((now.longValue() - milliseconds) / 3600000 < 72) {//三天内的文件加载到映射中
fileMap1.put(filePath, fileMap2.get(filePath));
}
}
}
}
}
return fileMap1;
}
/**
* @deprecated
* */
public Map<String,String> getFileMap(String srcPath,String filePath,Map<String,String> fileMap){
try{
File file = new File(filePath);
if(!file.exists()){
file.mkdirs();
}
for (File f:file.listFiles()) {
if(f.isDirectory()){
getFileMap(srcPath,f.getPath(),fileMap);
}else{
Long now = Long.valueOf((new Date()).getTime());
long milliseconds = f.lastModified();
if((now.longValue() - milliseconds) / 3600000 < 72) {//三天内的文件加载到映射中
fileMap.put(f.getPath(), f.getName());
}
}
}
}catch (Exception e){
e.printStackTrace();
}finally{
return fileMap;
}
}
/**
* 比对两个文件映射获取差异
* @param file1
* @param file2
* @param fileMap1
* @param fileMap2
* @return Map<String, File>
* @author banxian
* @date: 2021/1/13 16:36
*/
public Map<String, File> getUnCopyFileMap(Map<String, File> fileMap1, File file1, Map<String, File> fileMap2,
File file2) {
Map<String, File> fileMap = new HashMap<String, File>();
if (fileMap1 != null && fileMap2 != null) {
for (String filePath : fileMap1.keySet()) {
File srcFile = new File(filePath);
File srcParentDirectory = fileMap1.get(filePath).getParentFile();
String srcParentDirectoryPath = srcParentDirectory.getPath();
String midPath = srcParentDirectoryPath.substring(file1.getPath().length());
File fileMap2File = new File(file2.getPath() + "\\" + midPath + "\\" + srcFile.getName());
// System.out.println(srcParentDirectoryPath);
// System.out.println(midPath);
// System.out.println(fileMap2File.getPath());
if (fileMap2.get(fileMap2File.getPath()) == null) {
fileMap.put(filePath, srcFile);
}
}
}
return fileMap;
}
/**
* 根据映射复制文件
* @param fileMap
* @param toFile
* @param srcFile
* @return
* @author banxian
* @date: 2021/1/13 16:39
*/
public void copyFilesByFileMap(Map<String, File> fileMap, File srcFile, File toFile) {
FileUtil fileUtil = new FileUtil();
try {
Iterator e = fileMap.keySet().iterator();
while(e.hasNext()) {
String filePath = (String)e.next();
File srcSrcFile = new File(filePath);
File srcParentDirectory = srcSrcFile.getParentFile();
String srcParentDirectoryPath = srcParentDirectory.getPath();
String midPath = srcParentDirectoryPath.substring(srcFile.getPath().length());
File toFileDirectoty = new File(toFile.getPath() + "\\" + midPath);
if(!toFileDirectoty.exists()) {
toFileDirectoty.mkdirs();
}
File toToFile = new File(toFileDirectoty.getPath() + "\\" + srcSrcFile.getName());
fileUtil.copyFile(srcSrcFile, toToFile);
logger.info("复制" + srcSrcFile.getPath() + "到" + toToFile.getPath() + "完毕");
}
} catch (Exception var13) {
var13.printStackTrace();
}
}
/**
* 根据映射复制24小时内文件
* @param fileMap
* @param srcFile
* @param toFile
* @return
* @author banxian
* @date: 2021/1/13 16:38
*/
public void copyFilesByFileMap24Hour(Map<String, File> fileMap, File srcFile, File toFile) {
FileUtil fileUtil = new FileUtil();
try {
Iterator e = fileMap.keySet().iterator();
while(e.hasNext()) {
String filePath = (String)e.next();
File srcSrcFile = new File(filePath);
Long now = Long.valueOf((new Date()).getTime());
long milliseconds = srcSrcFile.lastModified();
if((now.longValue() - milliseconds) / 60000L < 1440L) {
File srcParentDirectory = srcSrcFile.getParentFile();
String srcParentDirectoryPath = srcParentDirectory.getPath();
String midPath = srcParentDirectoryPath.substring(srcFile.getPath().length());
File toFileDirectoty = new File(toFile.getPath() + "\\" + midPath);
if(!toFileDirectoty.exists()) {
toFileDirectoty.mkdirs();
}
File toToFile = new File(toFileDirectoty.getPath() + "\\" + srcSrcFile.getName());
fileUtil.copyFile(srcSrcFile, toToFile);
logger.info("复制" + srcSrcFile.getPath() + "到" + toToFile.getPath() + "完毕");
}
}
} catch (Exception var16) {
var16.printStackTrace();
}
}
/**
* 按有无复制文件夹专用
* @param srcPath
* @param toPath
* */
public void copyFilesByMap(String srcPath, String toPath) {
try {
File file1 = new File(srcPath);
File file2 = new File(toPath);
Map<String, File> fileMap1 = getFileMap(file1, null);
Map<String, File> fileMap2 = getFileMap(file2, null);
Map<String, File> fileMap = getUnCopyFileMap(fileMap1, file1, fileMap2, file2);
copyFilesByFileMap24Hour(fileMap, file1, file2);
System.out.println("从"+srcPath+"复制"+"到"+toPath+"完毕");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 将映射filePath--->fileName转换为filePath--->java.io.File
* @param fileMap
* @return
* @author banxian
* @date: 2021/1/12 17:12
*/
public Map<String, File> transFtpFileToFileMap(Map<String, String> fileMap){
URL url = TaskController.class.getClassLoader().getResource("application.yml");
java.io.File projectFile = new java.io.File(url.getFile());
Map<String,File> returnFileMap = new HashMap<String,File>();
try {
for(String filePath:fileMap.keySet()){
String fileName = fileMap.get(filePath);
String parentFilePath = projectFile.getParentFile().getPath()+"/temp"+filePath.substring(0,filePath.indexOf(fileName));
File parentfile = new File(parentFilePath.replaceAll("\\\\","/"));
if(!parentfile.exists()){
parentfile.mkdirs();
}
File file = new File(parentFilePath.replaceAll("\\\\","/")+"/"+fileName);
if(!file.exists()){
file.createNewFile();
}
returnFileMap.put(file.getPath(),file);
}
}catch (Exception e){
e.printStackTrace();
}
return returnFileMap;
}
/**
* 将映射filePath--->java.io.File转换为filePath--->fileName
* @param fileMap
* @return
* @author banxian
* @date: 2021/1/12 17:14
*/
public Map<String,String> transFileToFtpFileMap(String srcPath,Map<String, File> fileMap){
Map<String,String> returnFtpFileMap = new HashMap<String,String>();
try {
for(String filePath:fileMap.keySet()){
String fileName = fileMap.get(filePath).getName();
String ftpFilePath = filePath.substring(srcPath.length());
returnFtpFileMap.put(ftpFilePath,fileName);
}
}catch (Exception e){
e.printStackTrace();
}
return returnFtpFileMap;
}
}