文件对比8,单线程读,多线程对比,对比进度条,对比结果导出excel文件,已验收
介绍
文件对比其实是某个大项目的一个小部分功能的实现,
为了实现这个功能,我思路与想法很多都直接以blog的形式写了出来,可能部分会有缺漏(自认为无伤大雅),
我把这个功能单独拆出来(已经将部分信息隐去,你可自定包结构引入),
你可以做成jar包在linux服务器上单独使用也可以结合到你的业务当中,
技术不复杂但各种有趣的点很多,无论是多线程应用,md5加密,文件读取,文件递归,全局变量的应用
进度条的实现,还是以excel的形式导出结果 。
项目结构预览
base
util
CompareThreadUtil
import java.io.File;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class CompareThreadUtil {
public static int i;
private CountDownLatch latch;
private CyclicBarrier barrier;
private LinkedList linkedList = new LinkedList();
private LinkedList<Fc> linkedList1 = new LinkedList<Fc>();
private LinkedList<Fc> linkedList2 = new LinkedList<Fc>();
private Lock lock = new ReentrantLock();
private List listoneNot1 = new ArrayList<Fc>();
private List listoneNot2 = new ArrayList<Fc>();
private List listoneNot3 = new ArrayList<Fc>();
private Object numberLock = new Object();
private boolean lockboo = false;
private List<String> list;
private File root1;
private File root2;
private String dirName1;
private String dirName2;
private ReadFile2 trFile1;
private Boolean[] StopIt;
public CompareThreadUtil(CountDownLatch latch, CyclicBarrier barrier,
List list,
List<Fc> listoneNot1, List<Fc> listoneNot2,
List<Fc> listoneNot3, File root1, File root2,
String dirName1, String dirName2, ReadFile2 trFile1, Boolean[] stop
) {
this.latch = latch;
this.barrier = barrier;
this.list = list;
this.listoneNot1 = listoneNot1;
this.listoneNot2 = listoneNot2;
this.listoneNot3 = listoneNot3;
this.root1 = root1;
this.root2 = root2;
this.dirName1 = dirName1;
this.dirName2 = dirName2;
this.trFile1 = trFile1;
this.StopIt = stop;
}
public void addFileLevelAfter(int currentID, Integer percentagesum1, int[] i) {
File[] file = new File[1];
File f0File = null;
try {
while (true) {
trFile1.getFileInfo(file);
f0File = file[0];
if (!StopIt[0] && f0File == null) {
System.out.println("读取线程结束,线程" + currentID + "已获取不到文件,线程停止!");
break;
}
if (f0File != null) {
addPercent(i);
NumberFormat numberFormat = NumberFormat.getNumberInstance();
numberFormat.setMaximumFractionDigits(2);
String result = numberFormat.format((float) i[0] / (float) percentagesum1 * 100);
FileComparesServiceImpl.percentage1 = result + "%";
FileComparesServiceImpl.syncCurrentName1 = f0File.getName();
System.out.println("当前为检测线程" + currentID + "读取的文件为第" + i[0] + "个文件" + "文件名为" + f0File + " 文件总数为" + percentagesum1);
System.out.println("此时百分比为" + FileComparesServiceImpl.percentage1);
if (String.valueOf(f0File).startsWith(dirName1)) {
File file2 = new File(f0File.toString().replace(dirName1, dirName2));
if (file2.exists()) {
if (f0File.lastModified() != file2.lastModified()) {
Fc fc1 = new Fc();
fc1.setType("两数据源对比发生修改的文件");
fc1.setPath(f0File.getPath());
fc1.setLastmodified(String.valueOf(f0File.lastModified()));
fc1.setMd5(getMD5File(f0File));
listoneNot3.add(fc1);
}
} else {
Fc fc = new Fc();
fc.setType("数据源1发生删除的文件");
fc.setPath(f0File.getPath());
fc.setLastmodified(String.valueOf(f0File.lastModified()));
fc.setMd5(getMD5File(f0File));
listoneNot1.add(fc);
}
} else {
File file1 = new File(f0File.toString().replace(dirName2, dirName1));
if (file1.exists()) {
if (f0File.lastModified() != file1.lastModified()) {
Fc fc1 = new Fc();
fc1.setType("两数据源对比发生修改的文件");
fc1.setPath(f0File.getPath());
fc1.setLastmodified(String.valueOf(f0File.lastModified()));
fc1.setMd5(getMD5File(f0File));
listoneNot3.add(fc1);
}
} else {
Fc fc = new Fc();
fc.setType("数据源2发生删除的文件");
fc.setPath(f0File.getPath());
fc.setLastmodified(String.valueOf(f0File.lastModified()));
fc.setMd5(getMD5File(f0File));
listoneNot2.add(fc);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("线程" + currentID + " 正常停止!");
} finally {
latch.countDown();
}
}
public static synchronized void addPercent(int[] i) {
try {
i[0]++;
} catch (Exception e) {
e.printStackTrace();
}
}
}
ExcelUtil
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.context.annotation.ComponentScan;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class ExcelUtil {
public static void exportData(HttpServletResponse response, List<?> data, List<String> title, List<String> columns,
String excelName) {
response.setContentType("application/vnd.ms-excel");
String codedFileName = null;
OutputStream fOut = null;
try {
codedFileName = java.net.URLEncoder
.encode(excelName + "(" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + ")", "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
HSSFWorkbook wb = new HSSFWorkbook();
MyExportExcel myExportExcel = new MyExportExcel();
myExportExcel.setHssfWorkbook(wb);
myExportExcel.setExcelTitle(excelName + "(" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + ")",
title.size()-1);
myExportExcel.setNormalTwoRow(title);
if (data.size() < 1) {
myExportExcel.setNullData(columns.size()-1);
} else {
try {
myExportExcel.setExcelData2(data, columns);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
try {
fOut = response.getOutputStream();
wb.write(fOut);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fOut.flush();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void exportData1(HttpServletResponse response, List<?> data, List<String> title, List<String> columns,
String excelName) {
response.setContentType("application/vnd.ms-excel");
String codedFileName = null;
OutputStream fOut = null;
try {
codedFileName = java.net.URLEncoder
.encode(excelName + "(" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + ")", "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
HSSFWorkbook wb = new HSSFWorkbook();
MyExportExcel myExportExcel = new MyExportExcel();
myExportExcel.setHssfWorkbook(wb);
myExportExcel.setExcelTitle(excelName + "(" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + ")",
title.size()-1);
myExportExcel.setNormalTwoRow(title);
if (data.size() < 1) {
myExportExcel.setNullData(columns.size()-1);
} else {
try {
myExportExcel.setExcelData3(data, columns);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
try {
fOut = response.getOutputStream();
wb.write(fOut);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fOut.flush();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Md5
import java.io.File;
import java.io.FileInputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
public class Md5 {
public static String stringToMd5(String str) throws Exception {
StringBuffer userpass = new StringBuffer("");
if (str != null && !str.equals("")) {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes());
byte b[] = md.digest();
int i;
userpass = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
userpass.append("0");
userpass.append(Integer.toHexString(i));
}
}
return userpass.toString();
}
public static String getMD5File(File file) {
String inputFile = file.getPath();
String result = "";
int bufferSize = 256 * 1024;
FileInputStream fis = null;
DigestInputStream dis = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
fis = new FileInputStream(inputFile);
dis = new DigestInputStream(fis, md);
byte[] buffer = new byte[bufferSize];
while (dis.read(buffer) > 0)
;
md = dis.getMessageDigest();
byte[] resultByteArray = md.digest();
result = byteArrayToHex(resultByteArray);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
dis.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
private static String byteArrayToHex(byte[] b) {
String hs = "";
String stmp = "";
for (int i = 0; i < b.length; i++) {
stmp = (Integer.toHexString(b[i] & 0xFF));
if (stmp.length() == 1) {
hs = hs + "0" + stmp;
} else {
hs = hs + stmp;
}
if (i < b.length - 1) {
hs = hs + "";
}
}
return hs;
}
public static void main(String[] args) {
File files = new File("F:\\学习资料\\Spring Cloud微服务实战.pdf");
System.out.println(getMD5File(files));
}
}
ReadFile2
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ReadFile2 {
private CountDownLatch latch;
private CyclicBarrier barrier;
private LinkedList<File> linkedList = new LinkedList();
private Lock lock = new ReentrantLock();
private Object numberLock = new Object();
private boolean lockboo = false;
private List<String> list;
private File root1;
private File root2;
private String dirName1;
private String dirName2;
private Boolean[] StopIt;
public ReadFile2(CountDownLatch latch, CyclicBarrier barrier,
List list,
String dirName1, String dirName2,
Boolean[] stop
) {
this.latch = latch;
this.barrier = barrier;
this.list = list;
this.dirName1 = dirName1;
this.dirName2 = dirName2;
this.StopIt = stop;
}
public void readFile() {
try {
readFileInfo(new File(list.get(0)));
readFileInfo(new File(list.get(1)));
System.out.println("读取文件线程正常停止!");
} catch (Exception e) {
System.out.println("错误信息!" + e);
} finally {
StopIt[0] = false;
latch.countDown();
}
}
private void readFileInfo(File root) {
if (root.exists()) {
if (root.isDirectory()) {
File[] files = root.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
addFile(files[i]);
} else if (files[i].isDirectory()) {
readFileInfo(files[i]);
}
files[i] = null;
}
}
files = null;
} else if (root.isFile()) {
addFile(root);
}
}
}
private void addFile(File file) {
lock.lock();
try {
linkedList.addLast(file);
} catch (Exception e) {
System.out.println("添加文件出错:" + e);
} finally {
lock.unlock();
}
}
public void getFileInfo(File[] file) {
lock.lock();
try {
file[0] = linkedList.pollFirst();
if (lockboo && linkedList.size() <= 1000) {
synchronized (numberLock) {
lockboo = false;
numberLock.notify();
}
}
} catch (Exception e) {
System.out.println("获取文件出错:" + e);
} finally {
lock.unlock();
}
}
}
Util
import eu.bitwalker.useragentutils.UserAgent;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Util implements FilenameFilter {
private static final Logger logger = LoggerFactory.getLogger(Util.class);
private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private String suffix;
public Util(String suffix) {
super();
this.suffix = suffix;
}
public static String fileSizeFormat(long length) {
DecimalFormat df = new DecimalFormat("#0.0");
float size = (float) length / 1024;
String sumFilesize = "";
if (size >= 0 && size < 1) {
sumFilesize = length + " B";
} else if (size >= 1 && size < 1024) {
sumFilesize = df.format(size) + " K";
} else if (size >= 1024) {
size = size / 1024;
if (size >= 1 && size < 1024) {
sumFilesize = df.format(size) + " M";
} else if (size >= 1024 && size < 1048576) {
size = size / 1024;
sumFilesize = df.format(size) + " G";
} else if (size >= 1048576) {
size = size / 1024 / 1024;
sumFilesize = df.format(size) + " T";
}
}
return sumFilesize;
}
public static String fileSizeFormat2(long length) {
DecimalFormat df = new DecimalFormat("#0.0");
float size = (float) length / 1024;
String sumFilesize = "";
if (size >= 0 && size < 1) {
sumFilesize = length + " G";
} else if (size >= 1) {
sumFilesize = df.format(size) + " T";
}
return sumFilesize;
}
public static boolean fileCopy(String f1Path, String f2Path) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File f1 = null;
File f2 = null;
int outputSize = 1024000;
boolean boo = false;
try {
f1 = new File(f1Path);
f2 = new File(f2Path);
File pfile = new File(f2.getParent());
if (!pfile.exists()) {
pfile.mkdirs();
}
if (f1.length() > 104857600) {
outputSize = 10240000;
}
FileInputStream fis = new FileInputStream(f1);
FileOutputStream fos = new FileOutputStream(f2);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] b = new byte[outputSize];
int len = 0;
while ((len = bis.read(b, 0, b.length)) != -1) {
bos.write(b, 0, len);
}
bos.flush();
boo = true;
} catch (Exception e) {
boo = false;
logger.error("文件" + f1Path + "拷贝失败,原因:", e);
} finally {
try {
if (bos != null)
bos.close();
if (bis != null)
bis.close();
} catch (IOException e) {
boo = false;
e.printStackTrace();
}
if (boo) {
boo = f2.setLastModified(f1.lastModified());
}
}
return boo;
}
public static boolean fileCopyNew(File f1File, File f2File) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
int outputSize = 1024000;
boolean boo = false;
try {
File pfile = f2File.getParentFile();
if (!pfile.exists()) {
pfile.mkdirs();
}
pfile = null;
if (f1File.length() > 104857600) {
outputSize = 10240000;
}
FileInputStream fis = new FileInputStream(f1File);
FileOutputStream fos = new FileOutputStream(f2File);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] b = new byte[outputSize];
int len = 0;
while ((len = bis.read(b, 0, b.length)) != -1) {
bos.write(b, 0, len);
}
bos.flush();
boo = true;
} catch (Exception e) {
boo = false;
logger.error("文件" + f1File.getPath() + "拷贝失败,原因:", e);
} finally {
try {
if (bos != null)
bos.close();
if (bis != null)
bis.close();
} catch (IOException e) {
boo = false;
e.printStackTrace();
}
if (boo) {
boo = f2File.setLastModified(f1File.lastModified());
}
}
return boo;
}
public static void downloadZip(File file, HttpServletResponse response, boolean isDelete) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
boolean boo = false;
try {
response.reset();
response.setCharacterEncoding("ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes("gb2312"), "ISO8859-1"));
response.setContentType("application/octet-stream");
response.setHeader("Connection", "close");
response.setContentLength((int) file.length());
bis = new BufferedInputStream(new FileInputStream(file.getPath()));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] b = new byte[10240];
int len = 0;
while ((len = bis.read(b, 0, b.length)) != -1) {
bos.write(b, 0, len);
}
bos.flush();
boo = true;
} catch (Exception e) {
boo = false;
} finally {
try {
if (bos != null)
bos.close();
if (bis != null)
bis.close();
} catch (IOException e) {
boo = false;
}
if (boo && isDelete) {
file.delete();
}
}
}
public static boolean collectFiles(List<String> filePathList, String targetPath, String password) {
try {
File targetPathParent = new File(new File(targetPath).getParent());
if (!targetPathParent.exists()) {
targetPathParent.mkdirs();
}
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_STORE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_FASTEST);
if (password != null && !password.equals("")) {
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
parameters.setPassword(password);
}
ZipFile zipFile = new ZipFile(targetPath);
zipFile.setFileNameCharset("gbk");
for (String ss : filePathList) {
File file = new File(ss);
if (file.exists() && file.isDirectory()) {
zipFile.addFolder(file, parameters);
} else if (file.exists() && file.isFile()) {
zipFile.addFile(file, parameters);
}
}
} catch (ZipException e) {
logger.error("文件打包失败,原因:", e);
return false;
}
return true;
}
public static boolean extractFile(String sourceFile, String targetPath) {
try {
ZipFile zFile = new ZipFile(sourceFile);
zFile.setFileNameCharset("GBK");
if (!zFile.isValidZipFile()) {
throw new ZipException("压缩文件不合法,可能被损坏.");
}
File destDir = new File(targetPath);
if (destDir.isDirectory() && !destDir.exists()) {
destDir.mkdir();
}
zFile.extractAll(targetPath);
} catch (ZipException e) {
logger.error("文件解压失败,原因:", e);
return false;
}
return true;
}
public static boolean deleteFiles(String filePath) {
boolean flag = true;
File dirFile = new File(filePath);
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
File file = new File(files[i].getAbsolutePath());
flag = file.delete();
if (!flag)
break;
} else {
flag = deleteFiles(files[i].getAbsolutePath());
if (!flag)
break;
}
}
if (!flag)
return false;
if (dirFile.delete()) {
return true;
} else {
return false;
}
}
public static boolean fileShear(String source, String target) {
File sourceFile = new File(source);
boolean flag = true;
if (!sourceFile.exists()) {
return false;
}
if (!sourceFile.isDirectory()) {
if (fileCopy(source, target)) {
flag = sourceFile.delete();
} else {
flag = false;
}
return flag;
} else {
File[] files = sourceFile.listFiles();
for (int i = 0; i < files.length; i++) {
String filesPath = files[i].getPath().replace("\\", "/");
if (files[i].isFile()) {
if (fileCopy(filesPath, filesPath.replace(source, target))) {
flag = files[i].delete();
} else {
flag = false;
}
if (!flag)
break;
} else {
flag = fileShear(filesPath, filesPath.replace(source, target));
if (!flag)
break;
}
}
if (!flag)
return false;
if (sourceFile.delete()) {
return true;
} else {
return false;
}
}
}
public static String getProgressbar(int dividend, int divisor) {
if (divisor != 0) {
NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setMaximumFractionDigits(2);
return numberFormat.format((float) dividend / (float) divisor * 100);
} else {
return "0";
}
}
public static String getFileExtension(File file) {
String fileName = file.getName();
if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) {
return fileName.substring(fileName.lastIndexOf(".") + 1);
} else {
return "";
}
}
public static boolean isChinese(String countname) {
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(countname);
if (m.find()) {
return true;
}
return false;
}
public static String fileSizeFormatToG(double length) {
DecimalFormat df = new DecimalFormat("#0.0");
double size = length / 1024 / 1024 / 1024;
String sumFilesize = df.format(size);
return sumFilesize;
}
public static String creatId() {
UUID uuid = UUID.randomUUID();
return uuid.toString();
}
public static String transform(Integer num1, Integer num2) {
String result;
if (num1 != 0 && num2 != 0) {
NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setMaximumFractionDigits(2);
result = numberFormat.format((float) num1 / (float) num2 * 100);
} else {
result = "0";
}
return result;
}
public static String transform2(Long num1, Long num2) {
String result;
if (num1 != 0 && num2 != 0) {
NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setMaximumFractionDigits(0);
result = numberFormat.format((float) num1 / (float) num2 * 100);
} else {
result = "0";
}
return result;
}
public static double div(double d1, double d2, int len) {
BigDecimal b1 = new BigDecimal(d1);
BigDecimal b2 = new BigDecimal(d2);
return b1.divide(b2, len, BigDecimal.ROUND_HALF_UP).doubleValue();
}
public static void cutDirOrFile(String sourePath, String originalPath, String recyclePath) {
File file = new File(sourePath);
File[] files = file.listFiles();
for (File file2 : files) {
if (file2.isFile()) {
Util.fileCopyNew(file2, new File(file2.toString().replace("\\", "/").replace(originalPath, recyclePath)));
} else if (file2.isDirectory()) {
cutDirOrFile(file2.toString(), originalPath, recyclePath);
}
}
}
public static JSONArray readFolderToTree(String paths, JSONArray fileArray, String depotid, String flag) {
File file = new File(paths);
String[] filelist = file.list();
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < filelist.length; i++) {
JSONObject fileJson = new JSONObject();
File readfile = new File(paths + "/" + filelist[i]);
if (readfile.isDirectory()) {
String filepaths = readfile.getPath().replaceAll("\\\\", "/");
if (!filepaths.endsWith("/")) {
filepaths = filepaths + "/";
}
fileJson.put("id", filepaths);
fileJson.put("pId", paths);
fileJson.put("name", readfile.getName());
fileJson.put("state", "closed");
fileJson.put("isParent", "true");
JSONObject attributes = new JSONObject();
attributes.put("filepath", filepaths);
attributes.put("depotid", depotid);
attributes.put("flag", flag);
fileJson.put("attributes", attributes);
jsonArray.add(fileJson);
}
}
fileArray.addAll(jsonArray);
return fileArray;
}
@Override
public boolean accept(File file, String name) {
return !name.endsWith(suffix);
}
public static String subString(String path) {
String oo = path.substring(path.lastIndexOf("/", path.lastIndexOf("/") - 1) + 1);
return oo.substring(0, oo.lastIndexOf("/"));
}
public static String complementEight(Object a) {
String result = a + "";
if (!(a instanceof Integer)) {
return result;
}
if (a.toString().length() < 8) {
Integer cha = 8 - a.toString().length();
for (int i = 0; i < cha; i++) {
result = "0" + result;
}
}
return result;
}
@SuppressWarnings("static-access")
public static String getKey() {
String key = "";
return "77NEdEJJK3d396b4wx549LDY56S75bK1";
}
public static JSONObject getFileInfo(String filefullpath) {
File file = new File(filefullpath);
String filename;
String extname = "";
String filepath;
int nameindex = filefullpath.lastIndexOf("/");
filepath = filefullpath.substring(0, nameindex + 1);
filename = filefullpath.substring(nameindex + 1);
int extindex = filename.lastIndexOf(".");
if (extindex != -1) {
extname = filename.substring(extindex + 1);
filename = filename.substring(0, extindex);
}
JSONObject obj = new JSONObject();
obj.put("filepath", filepath);
obj.put("filename", filename);
obj.put("extname", extname);
return obj;
}
public static JSONObject getMessage(HttpServletRequest request){
UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
String brower = userAgent.getBrowser().getName();
String system = userAgent.getOperatingSystem().getName();
JSONObject jsonObject=new JSONObject();
jsonObject.put("system",system);
jsonObject.put("browser",brower);
return jsonObject;
}
public static void readCurrentFirstFile(String filepath, List<String> array) {
File file = new File(filepath);
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "/" + filelist[i]);
if (!readfile.isHidden()) {
array.add(readfile.getAbsolutePath());
}
}
}
public static Integer getCPUnum(){
int i = Runtime.getRuntime().availableProcessors();
return i;
}
public static boolean fileChannelCopy(File s, File t) {
FileInputStream fi = null;
FileOutputStream fo = null;
FileChannel in = null;
FileChannel out = null;
boolean boo = true;
int GB = 1024 * 1024 * 1024;
try {
File pfile = new File(t.getParent());
if (!pfile.exists()) {
pfile.mkdirs();
}
fi = new FileInputStream(s);
fo = new FileOutputStream(t);
in = fi.getChannel();
out = fo.getChannel();
if (in.size() / GB >= 2) {
ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024);
while (in.read(buffer) != -1) {
buffer.flip();
out.write(buffer);
buffer.clear();
}
} else {
in.transferTo(0, in.size(), out);
}
} catch (IOException e) {
boo = false;
logger.error("文件" + s.getPath() + "拷贝失败,原因:", e);
} finally {
try {
fi.close();
in.close();
fo.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
if (boo) {
boo = t.setLastModified(s.lastModified());
}
}
return boo;
}
public static String getFileExtensions(String fileName) {
if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) {
return fileName.substring(fileName.lastIndexOf(".") + 1);
} else {
return "";
}
}
}
Controller
FileCompareController
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.taiyusoft.tydms.base.util.ExcelUtil;
import com.taiyusoft.tydms.entity.Fc;
import com.taiyusoft.tydms.entity.ResponseForm;
import com.taiyusoft.tydms.service.FileComparesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/fileComparesimple")
public class FileCompareController {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private FileComparesService fileComparesService;
@RequestMapping("/selectfileComparesimple")
public void fileComparesimple(String firstpath, String secondpath) {
long time1 = System.currentTimeMillis();
File file1 = new File(firstpath);
File file2 = new File(secondpath);
try {
if (file1.isFile() || file1.isDirectory() && file2.isFile() || file2.isDirectory()) {
fileComparesService.fileComparesimple(firstpath.replace("/","\\"), secondpath.replace("/","\\"));
}
long time2 = System.currentTimeMillis();
int time = (int) ((time2 - time1) / 1000);
System.out.println("执行了:" + time + "秒!");
} catch (Exception e) {
e.printStackTrace();
}
}
@RequestMapping("/fileComparesinit")
public ResponseForm fileComparesinit(String firstpath, String secondpath) {
ResponseForm responseForm = new ResponseForm();
Integer sum = 0;
try {
sum = fileComparesService.initPercentage(firstpath, secondpath);
JSONObject jsonObject = new JSONObject();
jsonObject.put("sum", sum);
if (sum > 0) {
responseForm.setCode("200");
responseForm.setData(jsonObject);
responseForm.setMessage(ResponseForm.message.SUCCESS);
} else {
responseForm.setCode("401");
responseForm.setMessage(ResponseForm.message.FAIL);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e);
responseForm.setCode("402");
responseForm.setMessage(ResponseForm.message.FAIL);
}
return responseForm;
}
@RequestMapping("/fileComparesimplePercentage")
public ResponseForm fileComparesimplePercentage() {
ResponseForm responseForm = new ResponseForm();
JSONObject jsonObject;
try {
jsonObject = fileComparesService.fileComparesimplePercentage();
responseForm.setData(jsonObject);
responseForm.setCode("200");
responseForm.setMessage(ResponseForm.message.SUCCESS);
} catch (Exception e) {
responseForm.setCode("400");
responseForm.setMessage(ResponseForm.message.FAIL);
}
return responseForm;
}
@RequestMapping("/getfileCompares")
public ResponseForm getfileCompares() {
ResponseForm responseForm = new ResponseForm();
JSONObject jsonObject;
try {
jsonObject = fileComparesService.getfileCompares();
responseForm.setData(jsonObject);
responseForm.setCode("200");
responseForm.setMessage(ResponseForm.message.SUCCESS);
} catch (Exception e) {
responseForm.setCode("400");
responseForm.setMessage(ResponseForm.message.FAIL);
}
return responseForm;
}
@RequestMapping("/exportExcel")
@ResponseBody
public String exportExcel(HttpServletResponse response) {
ResponseForm responseForm = new ResponseForm();
String querystr = "";
String starTime = "";
String endTime = "";
JSONObject jsonObject;
responseForm = getfileCompares();
ArrayList<Fc> listoneNot1 = new ArrayList<>();
ArrayList<Fc> listoneNot2 = new ArrayList<>();
ArrayList<Fc> listoneNot3 = new ArrayList<>();
JSONObject data = new JSONObject();
listoneNot1 = (ArrayList<Fc>) ((JSONObject) responseForm.getData()).get("数据源1发生删除的文件");
listoneNot2 = (ArrayList<Fc>) ((JSONObject) responseForm.getData()).get("数据源2发生删除的文件");
listoneNot3 = (ArrayList<Fc>) ((JSONObject) responseForm.getData()).get("两数据源对比发生修改的文件");
ArrayList<Fc> fcs = new ArrayList<>();
fcs.addAll(listoneNot1);
fcs.addAll(listoneNot2);
fcs.addAll(listoneNot3);
for (Fc fc : fcs) {
System.out.println(fc);
}
List<String> list1 = new ArrayList<>();
list1.add("文件类别");
list1.add("路径");
list1.add("最后修改时间");
list1.add("md5");
List<String> list2 = new ArrayList<>();
list2.add("type");
list2.add("path");
list2.add("lastmodified");
list2.add("md5");
ExcelUtil.exportData(response, fcs, list1, list2, "对比文件结果");
try {
response.getWriter().print("导出成功");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
enttity
Fc
public class Fc {
String type;
String path;
String lastmodified;
String md5;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getLastmodified() {
return lastmodified;
}
public void setLastmodified(String lastmodified) {
this.lastmodified = lastmodified;
}
public String getMd5() {
return md5;
}
public void setMd5(String md5) {
this.md5 = md5;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Fc fc = (Fc) o;
if (path != null ? !path.equals(fc.path) : fc.path != null) return false;
if (lastmodified != null ? !lastmodified.equals(fc.lastmodified) : fc.lastmodified != null) return false;
return md5 != null ? md5.equals(fc.md5) : fc.md5 == null;
}
@Override
public int hashCode() {
int result = path != null ? path.hashCode() : 0;
result = 31 * result + (lastmodified != null ? lastmodified.hashCode() : 0);
result = 31 * result + (md5 != null ? md5.hashCode() : 0);
return result;
}
}
MyExportExcel
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class MyExportExcel {
private HSSFWorkbook hssfWorkbook;
private HSSFSheet sheet;
public HSSFWorkbook getHssfWorkbook() {
return hssfWorkbook;
}
public HSSFSheet getSheet() {
return sheet;
}
public void setSheet(HSSFSheet sheet) {
this.sheet = sheet;
}
public void setHssfWorkbook(HSSFWorkbook hssfWorkbook) {
this.hssfWorkbook = hssfWorkbook;
}
@SuppressWarnings("deprecation")
public void setExcelTitle(String title, int colSum) {
this.sheet = hssfWorkbook.createSheet(title);
for (int i = 0; i <= colSum; i++) {
sheet.setColumnWidth((short) i, (short) 7250);
}
HSSFCellStyle cellStyle = hssfWorkbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle.setWrapText(true);
cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font = hssfWorkbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontName("宋体");
font.setFontHeight((short) 300);
cellStyle.setFont(font);
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
row.setHeight((short) 600);
cell.setCellType(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(new HSSFRichTextString(title));
sheet.addMergedRegion(new CellRangeAddress(0, (short) 0, 0, (short) colSum));
cell.setCellStyle(cellStyle);
}
public void setNullData(int colSum) {
HSSFCellStyle cellStyle = hssfWorkbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
HSSFRow row = sheet.createRow(2);
for (int i = 0; i <= colSum; i++) {
HSSFCell cell2 = row.createCell(i);
cell2.setCellStyle(cellStyle);
}
HSSFCell cell = row.createCell(0);
row.setHeight((short) 600);
cell.setCellType(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(new HSSFRichTextString("没有数据"));
sheet.addMergedRegion(new CellRangeAddress((short) 2, (short) 0, 2, (short) colSum));
cell.setCellStyle(cellStyle);
}
public void setNormalTwoRow(List<String> columHeader) {
HSSFRow row1 = sheet.createRow(1);
row1.setHeight((short) 400);
HSSFCellStyle cellStyle = hssfWorkbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
HSSFFont font = hssfWorkbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellStyle.setFont(font);
for (int i = 0; i < columHeader.size(); i++) {
HSSFCell cell3 = row1.createCell(i);
cell3.setCellType(HSSFCell.ENCODING_UTF_16);
cell3.setCellStyle(cellStyle);
cell3.setCellValue(new HSSFRichTextString(columHeader.get(i)));
}
}
public void setExcelData(List<?> data, List<String> method) {
HSSFCellStyle cellStyle = hssfWorkbook.createCellStyle();
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
try {
for (int j = 0; j < data.size(); j++) {
HSSFRow row = sheet.createRow(2 + j);
Class<?> clazz = data.get(j).getClass();
for (int i = 0; i < method.size(); i++) {
HSSFCell cell = row.createCell(i);
cell.setCellType(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
java.lang.reflect.Method m = clazz.getMethod(method.get(i));
if (m.invoke(data.get(j)) != null) {
cell.setCellValue(m.invoke(data.get(j)).toString());
} else {
cell.setCellValue("");
}
}
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public void setExcelData2(List<?> list, List<String> colname) throws IllegalAccessException {
HSSFCellStyle cellStyle = hssfWorkbook.createCellStyle();
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
for (int j = 0; j < list.size(); j++) {
HSSFRow row = sheet.createRow(2 + j);
Map<String, Object> map;
if(list.get(j).getClass().equals(HashMap.class))
{
map = (Map<String, Object>)list.get(j);
}else {
map = objectToMap(list.get(j));
System.out.println(map.toString());
}
System.out.println(map.toString());
for (int i = 0; i < colname.size(); i++) {
HSSFCell cell = row.createCell(i);
cell.setCellType(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
Object object = map.get(colname.get(i));
if (object == null) {
cell.setCellValue("");
} else {
cell.setCellValue(new HSSFRichTextString(object.toString()));
}
}
}
}
public void setExcelData2(List<?> list, List<String> colname, int startnum) throws IllegalAccessException {
HSSFCellStyle cellStyle = hssfWorkbook.createCellStyle();
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
for (int j = startnum; j < list.size(); j++) {
HSSFRow row = sheet.createRow(2 + j);
Map<String, Object> map = objectToMap(list.get(j));
for (int i = 0; i < colname.size(); i++) {
HSSFCell cell = row.createCell(i);
cell.setCellType(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
Object object = map.get(colname.get(i));
if (object == null) {
cell.setCellValue("");
} else {
cell.setCellValue(new HSSFRichTextString(object.toString()));
}
}
}
}
public void setExcelData2(List<?> list, List<String> colname, Map<String, Map<String, String>> change)
throws IllegalAccessException {
HSSFCellStyle cellStyle = hssfWorkbook.createCellStyle();
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
for (int j = 0; j < list.size(); j++) {
HSSFRow row = sheet.createRow(2 + j);
Map<String, Object> map = objectToMap(list.get(j));
for (int i = 0; i < colname.size(); i++) {
HSSFCell cell = row.createCell(i);
cell.setCellType(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
Object object = map.get(colname.get(i));
if (object == null) {
cell.setCellValue("");
} else {
for (Entry<String, Map<String, String>> eny : change.entrySet()) {
String fname = eny.getKey();
Map<String, String> fval = eny.getValue();
if (fname.equals(colname.get(i))) {
cell.setCellValue(new HSSFRichTextString(fval.get(object.toString())));
} else {
cell.setCellValue(new HSSFRichTextString(object.toString()));
}
}
}
}
}
}
public void setExcelData3(List<?> list, List<String> colname) throws IllegalAccessException {
HSSFCellStyle cellStyle = hssfWorkbook.createCellStyle();
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
for (int j = 0; j < list.size(); j++) {
HSSFRow row = sheet.createRow(2 + j);
Map<String, String> map = (Map<String, String>) list.get(j);
System.out.println(map.toString());
for (int i = 0; i < colname.size(); i++) {
HSSFCell cell = row.createCell(i);
cell.setCellType(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
Object object = map.get(colname.get(i));
if (object == null) {
cell.setCellValue("");
} else {
cell.setCellValue(new HSSFRichTextString(object.toString()));
}
}
}
}
public static Map<String, Object> objectToMap(Object obj) {
Map<String, Object> reMap = new HashMap<String, Object>();
if (obj == null)
return null;
try {
Class<?> objClass = obj.getClass();
while (objClass != null) {
Field[] fields = objClass.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
try {
Field f = objClass.getDeclaredField(fields[i].getName());
f.setAccessible(true);
Object o = f.get(obj);
reMap.put(fields[i].getName(), o);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
objClass = objClass.getSuperclass();
}
} catch (SecurityException e) {
e.printStackTrace();
}
return reMap;
}
}
ResponseForm
import java.io.Serializable;
public class ResponseForm implements Serializable {
private Object data;
private String code;
private message message;
private Integer total;
private Integer limit;
public Integer getLimit() {
return limit;
}
public void setLimit(Integer limit) {
this.limit = limit;
}
public enum message {
SUCCESS,
FAIL
}
public ResponseForm() {
}
public ResponseForm(Object data, String code, ResponseForm.message message, Integer total) {
this.data = data;
this.code = code;
this.message = message;
this.total = total;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public ResponseForm.message getMessage() {
return message;
}
public void setMessage(ResponseForm.message message) {
this.message = message;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
}
service
import com.alibaba.fastjson.JSONObject;
public interface FileComparesService {
void fileComparesimple(String firstpath, String secondpath);
Integer initPercentage(String firstpath, String secondpath);
JSONObject fileComparesimplePercentage();
JSONObject getfileCompares();
}
serviceimpl
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Service
public class FileComparesServiceImpl implements FileComparesService {
private static final Logger logger = Logger.getLogger(FileComparesServiceImpl.class);
public static String syncCurrentName1 = null;
public static String percentage1;
public static Integer percentagesum1 = null;
public static int[] countfile;
public static final ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();
private List listoneNot1 = new ArrayList<Fc>();
private List listoneNot2 = new ArrayList<Fc>();
private List listoneNot3 = new ArrayList<Fc>();
public static boolean flag;
private Boolean[] scanStop = new Boolean[1];
@Override
public void fileComparesimple(String firstpath, String secondpath) {
long startTime = System.currentTimeMillis();
int cpuCount = Util.getCPUnum();
cpuCount = cpuCount > 10 ? 10 : 5;
countfile = new int[]{
0};
String dirName1 = firstpath;
File root1 = new File(dirName1);
String dirName2 = secondpath;
File root2 = new File(dirName2);
listoneNot1 = new ArrayList<Fc>();
listoneNot2 = new ArrayList<Fc>();
listoneNot3 = new ArrayList<Fc>();
scanStop[0] = true;
List list = new ArrayList();
list.add(dirName1);
list.add(dirName2);
logger.info("分配线程开始...");
CountDownLatch latchs = new CountDownLatch(cpuCount + 1);
ExecutorService exec = Executors.newCachedThreadPool();
final ReadFile2 trFile1 = new ReadFile2(
latchs, null, list,
dirName1, dirName2, scanStop);
logger.info("读取文件线程启动...");
exec.execute(new Thread() {
@Override
public void run() {
trFile1.readFile();
}
});
for (int i = 0; i < cpuCount; i++) {
final CompareThreadUtil thDao = new CompareThreadUtil(
latchs, null, list, listoneNot1,
listoneNot2, listoneNot3, root1,
root2, dirName1, dirName2, trFile1, scanStop);
System.out.println("检测线程" + i + " 启动...");
final int currentID = i;
exec.execute(new Thread() {
@Override
public void run() {
thDao.addFileLevelAfter(currentID, percentagesum1, countfile);
}
});
}
try {
exec.shutdown();
latchs.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
long endTime1 = System.currentTimeMillis();
System.out.println("对比运行时间:" + (double) (endTime1 - startTime) / 1000 + "s");
if (listoneNot1.size() > 0) {
logger.info("数据1删除的文件如下");
for (int i = 0; i < listoneNot1.size(); i++) {
logger.info(((Fc) listoneNot1.get(0)).getPath());
}
} else {
logger.info("数据1无删除的文件...");
}
if (listoneNot2.size() > 0) {
logger.info("数据2删除的文件如下");
for (int i = 0; i < listoneNot2.size(); i++) {
logger.info(((Fc) listoneNot2.get(i)).getPath());
}
} else {
logger.info("数据2无删除文件");
}
if (listoneNot3.size() > 0) {
logger.info("两数据源之间发生修改的文件如下");
for (int i = 0; i < listoneNot3.size(); i++) {
logger.info(((Fc) listoneNot3.get(i)).getPath());
}
} else {
logger.info("两数据源之间无发生修改的文件");
}
long endTime = System.currentTimeMillis();
System.out.println("程序运行时间:" + (double) (endTime - startTime) / 1000 + "s");
JSONObject jsonObject = new JSONObject();
jsonObject.put("数据源1发生删除的文件", listoneNot1);
jsonObject.put("数据源2发生删除的文件", listoneNot2);
jsonObject.put("两数据源对比发生修改的文件", listoneNot3);
flag = true;
jsonObject.put("flag", flag);
}
@Override
public Integer initPercentage(String firstpath, String secondpath) {
flag = false;
percentagesum1 = 0;
percentage1 = "";
syncCurrentName1 = "";
readFileInfo(new File(firstpath));
readFileInfo(new File(secondpath));
return percentagesum1;
}
@Override
public JSONObject fileComparesimplePercentage() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("百分比", percentage1);
jsonObject.put("当前进行到的文件", syncCurrentName1);
jsonObject.put("flag", flag);
return jsonObject;
}
@Override
public JSONObject getfileCompares() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("数据源1发生删除的文件", listoneNot1);
jsonObject.put("数据源2发生删除的文件", listoneNot2);
jsonObject.put("两数据源对比发生修改的文件", listoneNot3);
return jsonObject;
}
private void readFileInfo(File root) {
if (root.exists()) {
if (root.isDirectory()) {
File[] files = root.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
percentagesum1++;
} else if (files[i].isDirectory()) {
readFileInfo(files[i]);
}
files[i] = null;
}
}
files = null;
} else if (root.isFile()) {
percentagesum1++;
}
}
}
}
helloWorldApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication( exclude= DataSourceAutoConfiguration.class)
public class helloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(helloWorldApplication.class, args);
}
}