关于利用ExactFile检查是不是完整的md5加密文件
2015-11-30 00:00
253 查看
摘要: 由于工作中经常要检查服务器上的文件是不是下载下来的文件是完整的没有错误的,或者说没有被修改过的,所以运用了ExactFile这个软件是检查,他是不是完整的
首先我们来认识一下,ExactFile是一个很好用的一个软件,他支持手动生成md5加密,监测是否完整。不懂的可以去官网看一下官网网址是:http://www.exactfile.com/
好了废话不多说,我们今天的主题是:如何用java调用直接读取检查,不用手动的打开软件监测。运行java生成的可以运行 .jar文件完成文件的监测完整性,是否有改动过等。
首先我们要去官网把ExactFile的命令行执行文件下载下来到本地,并且把下载下来的exf文件解压,把解压的文件加入到系统运行环境变量中。
下载如图:
![](http://static.oschina.net/uploads/space/2015/1130/094329_GGUj_1537424.png)
解压后如图:
![](http://static.oschina.net/uploads/space/2015/1130/094647_kZXM_1537424.png)
然后我们加入系统环境变量中:计算机->右键属性->高级系统设置->环境变量->Path->黏贴在最后(记得分红隔开前面的和后面的;地址要根据自己解压的exf文件路径修改为自己的路径加入到环境变量中)
所有的操作如图:
![](http://static.oschina.net/uploads/space/2015/1130/100031_Lu2w_1537424.png)
![](http://static.oschina.net/uploads/space/2015/1130/095823_bjty_1537424.png)
![](http://static.oschina.net/uploads/space/2015/1130/095823_ohfu_1537424.png)
我们在cmd中测试一下是否成功加入到环境变量,直接输入:exf
成功如图所示:
![](http://static.oschina.net/uploads/space/2015/1130/100458_7WEt_1537424.png)
到此我们已经安装成功。
好了进入代码主题部分
废话不多少上代码,由于是一个小功能所以写的比较凌乱,大家就凑合着看,
主要的难点就是java如何调用cmd命令行运行命令。可以根据。jar所在的目录获取当前目录执行检查。
代码如下:
.properties配置文件如下:
isRelativePath=y
filePath=C:\\Users\\Administrator\\Desktop\\testdata\\
outPath=C:\\Users\\Administrator\\Desktop\\testdata\\
代码:
import java.io.File;
import java.util.Map;
import com.zhs.file.verify.utils.PropertiesUtil;
import com.zhs.file.verify.utils.ReadFile;
import com.zhs.file.verify.utils.TraverseFile;
public class Execute {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
group();
}
public static void group() throws Exception{
Map<String, String> filePathMap = PropertiesUtil.getReplaceStr();//读取。.properties配置文件
ativePath = filePathMap.get("isRelativePath").toLowerCase();//判断配置的是绝对路径还是相对路径
String filePath = filePathMap.get("filePath"); //给我你要读取的文件夹路径
File outPath = new File(filePathMap.get("outPath")); //随便给一个输出文件夹的路径(不存在也可以)
String fileCmd = System.getProperty("java.class.path");//获取当前运行的文件路径
File file = new File(fileCmd);
String absolutePath = file.getAbsolutePath();//获取文件绝对路径 难点 这个方法是获得文件绝对路径的 其他方法就只能获得文件名
String fileNameSuffix = filePath;
if(!isRelativePath.equals("y")){//判断配置的是绝对路径还是相对路径
num = ReadFile.readFolder(filePath,outPath);
}else{
String str = System.getProperties().getProperty("file.separator");
if(str.equals("\\")){//判断是win还是linux系统
System.out.println("running windows system");
if (absolutePath.indexOf("\\") >= 0) {
fileNameSuffix = absolutePath.substring(0, absolutePath.lastIndexOf("\\"));
}
fileNameSuffix = fileNameSuffix + "\\";
}else{
System.out.println("running linux system");
if (absolutePath.indexOf("/") >= 0) {
fileNameSuffix = absolutePath.substring(0, absolutePath.lastIndexOf("/"));
}
fileNameSuffix = fileNameSuffix + "/";
}
System.out.println("fileNameSuffix:"+fileNameSuffix);
}
TraverseFile.readFolder(fileNameSuffix);//检查是否是正确的.md5文件
}
}
调用cmd 运行cxf 检查服务器下载下来的md5文件:
import java.io.File;
public class TraverseFile {
/**
* 读取文件夹 遍历此文件夹下的所有文件 包括子文件夹里的.md5文件执行 匹配命令
* @return int 以操作的文件数量
*/
public static int readFolder(String filePath){
int num = 0;//记录创建成功的
try {
//读取指定文件夹下的所有文件
File file = new File(filePath);
if (!file.isDirectory()) {
//System.out.println("---------- 该文件不是一个目录文件 ----------");
} else if (file.isDirectory()) {
//System.out.println("---------- 很好,这是一个目录文件夹 ----------");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filePath + "\\" + filelist[i]);
//String path = readfile.getPath();//文件路径
String absolutepath = readfile.getAbsolutePath();//文件的绝对路径
String filename = readfile.getName();//读到的文件名
if(readfile.isDirectory()){
readFolder(absolutepath);
}
boolean boo = true;
if (filename.indexOf("_") >= 0) {
String okMd5 = filename.substring(filename.lastIndexOf("_"), filename.length());
System.out.println("absolutepath:"+absolutepath);
if (okMd5.equals("_ok.md5") || okMd5.equals("_error.md5")) {
boo = false;
}
}
if (filename.indexOf(".") >= 0) {
String fileNameSuffix = filename.substring(filename.lastIndexOf("."), filename.length());
if (fileNameSuffix.equals(".md5") && boo) {
num ++;
String command = "exf -c "+absolutepath;
Command.exeCmd(command,absolutepath,filename);;//调用 readFile 方法读取文件夹下所有文件
}else {
//System.out.println("---------- 没有.md5后缀文件需要操作 ----------文件名:"+filename);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("---------- 所有文件操作完毕 ----------已经操作的文件数量为:"+num);
return num;
}
}
调用cmd命令运行exf 检查如有错误修改文件名称_errror.md5,生成。log日志; 没有错误就修改为_ok.md5
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Command {
public static void exeCmd(String commandStr,String filePath,String fileName) throws Exception {
BufferedReader br = null;
System.out.println(commandStr);
try {
Process p = Runtime.getRuntime().exec(commandStr);
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
StringBuilder sb = new StringBuilder();
int index = 0;
String str = "";
while ((line = br.readLine()) != null) {
index ++;
str = line;
sb.append(line + "\n");
}
System.out.println(sb.toString());
if(str.equals("No errors.")){
ModifyFileName.modify(filePath, "_ok");
}else{
ModifyFileName.modify(filePath, "_error");
if (filePath.indexOf(".") >= 0) {
filePath = filePath.substring(0, filePath.lastIndexOf("."));
}
filePath = filePath + "_error.log";
ModifyFileName.creatLogAndWriteContent(filePath, sb.toString());
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if (br != null){
br.close();
}
}
}
}
修改文件名称生成文件工具类:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ModifyFileName {
/**
* 修改文件名称
* @param absolutePath 文件绝对路径
* @param isOkOrErrorPix 希望加的是_ok 或是 _error
*/
public static void modify(String absolutePath,String isOkOrErrorPix){
File file = new File(absolutePath); // 指定文件名及路径
//String name = "_ok";
String name = isOkOrErrorPix;
String filename = file.getAbsolutePath();
if (filename.indexOf(".") >= 0) {
filename = filename.substring(0, filename.lastIndexOf("."));
}
if(file.renameTo(new File(filename+name + ".md5"))){ // 改名
System.out.println("success! file name:"+filename+".md5");
}else{
System.out.println("false! file name:"+filename+".md5");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String name = "C:\\Users\\Administrator\\Desktop\\files\\checksum.md5";
// modify(name);
}
/**
* 生成.log文件并写入内容
* @param absolutePath
* @param isOkOrErrorPix
* @throws Exception
*/
public static void creatLogAndWriteContent(String absolutePath,String content) throws Exception{
FileWriter fw = null;
BufferedWriter bw = null;
File file = new File(absolutePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(new Date());
file.createNewFile();
fw = new FileWriter(file, true);
bw = new BufferedWriter(fw);
bw.write(content+" " + date + "\n\n\n");
} catch (Exception e) {
e.printStackTrace();
}finally{
if(bw!=null){
bw.flush();
bw.close();
}
if(fw!=null){
fw.close();
}
}
}
}
到此我们已经完成整个检验工作:配上运行截图
服务器下载下来的文件如下所示:
![](http://static.oschina.net/uploads/space/2015/1130/103154_XoUa_1537424.png)
![](http://static.oschina.net/uploads/space/2015/1130/103751_73AO_1537424.png)
好了我们已经完成了对java调用cmd命令行运行EXACTFILE的检查文件并生成日志的运用,希望对有需要的人有一点点的帮助
首先我们来认识一下,ExactFile是一个很好用的一个软件,他支持手动生成md5加密,监测是否完整。不懂的可以去官网看一下官网网址是:http://www.exactfile.com/
好了废话不多说,我们今天的主题是:如何用java调用直接读取检查,不用手动的打开软件监测。运行java生成的可以运行 .jar文件完成文件的监测完整性,是否有改动过等。
首先我们要去官网把ExactFile的命令行执行文件下载下来到本地,并且把下载下来的exf文件解压,把解压的文件加入到系统运行环境变量中。
下载如图:
![](http://static.oschina.net/uploads/space/2015/1130/094329_GGUj_1537424.png)
解压后如图:
![](http://static.oschina.net/uploads/space/2015/1130/094647_kZXM_1537424.png)
然后我们加入系统环境变量中:计算机->右键属性->高级系统设置->环境变量->Path->黏贴在最后(记得分红隔开前面的和后面的;地址要根据自己解压的exf文件路径修改为自己的路径加入到环境变量中)
所有的操作如图:
![](http://static.oschina.net/uploads/space/2015/1130/100031_Lu2w_1537424.png)
![](http://static.oschina.net/uploads/space/2015/1130/095823_bjty_1537424.png)
![](http://static.oschina.net/uploads/space/2015/1130/095823_ohfu_1537424.png)
我们在cmd中测试一下是否成功加入到环境变量,直接输入:exf
成功如图所示:
![](http://static.oschina.net/uploads/space/2015/1130/100458_7WEt_1537424.png)
到此我们已经安装成功。
好了进入代码主题部分
废话不多少上代码,由于是一个小功能所以写的比较凌乱,大家就凑合着看,
主要的难点就是java如何调用cmd命令行运行命令。可以根据。jar所在的目录获取当前目录执行检查。
代码如下:
.properties配置文件如下:
isRelativePath=y
filePath=C:\\Users\\Administrator\\Desktop\\testdata\\
outPath=C:\\Users\\Administrator\\Desktop\\testdata\\
代码:
import java.io.File;
import java.util.Map;
import com.zhs.file.verify.utils.PropertiesUtil;
import com.zhs.file.verify.utils.ReadFile;
import com.zhs.file.verify.utils.TraverseFile;
public class Execute {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
group();
}
public static void group() throws Exception{
Map<String, String> filePathMap = PropertiesUtil.getReplaceStr();//读取。.properties配置文件
ativePath = filePathMap.get("isRelativePath").toLowerCase();//判断配置的是绝对路径还是相对路径
String filePath = filePathMap.get("filePath"); //给我你要读取的文件夹路径
File outPath = new File(filePathMap.get("outPath")); //随便给一个输出文件夹的路径(不存在也可以)
String fileCmd = System.getProperty("java.class.path");//获取当前运行的文件路径
File file = new File(fileCmd);
String absolutePath = file.getAbsolutePath();//获取文件绝对路径 难点 这个方法是获得文件绝对路径的 其他方法就只能获得文件名
String fileNameSuffix = filePath;
if(!isRelativePath.equals("y")){//判断配置的是绝对路径还是相对路径
num = ReadFile.readFolder(filePath,outPath);
}else{
String str = System.getProperties().getProperty("file.separator");
if(str.equals("\\")){//判断是win还是linux系统
System.out.println("running windows system");
if (absolutePath.indexOf("\\") >= 0) {
fileNameSuffix = absolutePath.substring(0, absolutePath.lastIndexOf("\\"));
}
fileNameSuffix = fileNameSuffix + "\\";
}else{
System.out.println("running linux system");
if (absolutePath.indexOf("/") >= 0) {
fileNameSuffix = absolutePath.substring(0, absolutePath.lastIndexOf("/"));
}
fileNameSuffix = fileNameSuffix + "/";
}
System.out.println("fileNameSuffix:"+fileNameSuffix);
}
TraverseFile.readFolder(fileNameSuffix);//检查是否是正确的.md5文件
}
}
调用cmd 运行cxf 检查服务器下载下来的md5文件:
import java.io.File;
public class TraverseFile {
/**
* 读取文件夹 遍历此文件夹下的所有文件 包括子文件夹里的.md5文件执行 匹配命令
* @return int 以操作的文件数量
*/
public static int readFolder(String filePath){
int num = 0;//记录创建成功的
try {
//读取指定文件夹下的所有文件
File file = new File(filePath);
if (!file.isDirectory()) {
//System.out.println("---------- 该文件不是一个目录文件 ----------");
} else if (file.isDirectory()) {
//System.out.println("---------- 很好,这是一个目录文件夹 ----------");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filePath + "\\" + filelist[i]);
//String path = readfile.getPath();//文件路径
String absolutepath = readfile.getAbsolutePath();//文件的绝对路径
String filename = readfile.getName();//读到的文件名
if(readfile.isDirectory()){
readFolder(absolutepath);
}
boolean boo = true;
if (filename.indexOf("_") >= 0) {
String okMd5 = filename.substring(filename.lastIndexOf("_"), filename.length());
System.out.println("absolutepath:"+absolutepath);
if (okMd5.equals("_ok.md5") || okMd5.equals("_error.md5")) {
boo = false;
}
}
if (filename.indexOf(".") >= 0) {
String fileNameSuffix = filename.substring(filename.lastIndexOf("."), filename.length());
if (fileNameSuffix.equals(".md5") && boo) {
num ++;
String command = "exf -c "+absolutepath;
Command.exeCmd(command,absolutepath,filename);;//调用 readFile 方法读取文件夹下所有文件
}else {
//System.out.println("---------- 没有.md5后缀文件需要操作 ----------文件名:"+filename);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("---------- 所有文件操作完毕 ----------已经操作的文件数量为:"+num);
return num;
}
}
调用cmd命令运行exf 检查如有错误修改文件名称_errror.md5,生成。log日志; 没有错误就修改为_ok.md5
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Command {
public static void exeCmd(String commandStr,String filePath,String fileName) throws Exception {
BufferedReader br = null;
System.out.println(commandStr);
try {
Process p = Runtime.getRuntime().exec(commandStr);
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
StringBuilder sb = new StringBuilder();
int index = 0;
String str = "";
while ((line = br.readLine()) != null) {
index ++;
str = line;
sb.append(line + "\n");
}
System.out.println(sb.toString());
if(str.equals("No errors.")){
ModifyFileName.modify(filePath, "_ok");
}else{
ModifyFileName.modify(filePath, "_error");
if (filePath.indexOf(".") >= 0) {
filePath = filePath.substring(0, filePath.lastIndexOf("."));
}
filePath = filePath + "_error.log";
ModifyFileName.creatLogAndWriteContent(filePath, sb.toString());
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if (br != null){
br.close();
}
}
}
}
修改文件名称生成文件工具类:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ModifyFileName {
/**
* 修改文件名称
* @param absolutePath 文件绝对路径
* @param isOkOrErrorPix 希望加的是_ok 或是 _error
*/
public static void modify(String absolutePath,String isOkOrErrorPix){
File file = new File(absolutePath); // 指定文件名及路径
//String name = "_ok";
String name = isOkOrErrorPix;
String filename = file.getAbsolutePath();
if (filename.indexOf(".") >= 0) {
filename = filename.substring(0, filename.lastIndexOf("."));
}
if(file.renameTo(new File(filename+name + ".md5"))){ // 改名
System.out.println("success! file name:"+filename+".md5");
}else{
System.out.println("false! file name:"+filename+".md5");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String name = "C:\\Users\\Administrator\\Desktop\\files\\checksum.md5";
// modify(name);
}
/**
* 生成.log文件并写入内容
* @param absolutePath
* @param isOkOrErrorPix
* @throws Exception
*/
public static void creatLogAndWriteContent(String absolutePath,String content) throws Exception{
FileWriter fw = null;
BufferedWriter bw = null;
File file = new File(absolutePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(new Date());
file.createNewFile();
fw = new FileWriter(file, true);
bw = new BufferedWriter(fw);
bw.write(content+" " + date + "\n\n\n");
} catch (Exception e) {
e.printStackTrace();
}finally{
if(bw!=null){
bw.flush();
bw.close();
}
if(fw!=null){
fw.close();
}
}
}
}
到此我们已经完成整个检验工作:配上运行截图
服务器下载下来的文件如下所示:
![](http://static.oschina.net/uploads/space/2015/1130/103154_XoUa_1537424.png)
![](http://static.oschina.net/uploads/space/2015/1130/103751_73AO_1537424.png)
好了我们已经完成了对java调用cmd命令行运行EXACTFILE的检查文件并生成日志的运用,希望对有需要的人有一点点的帮助
相关文章推荐
- android 按home键返回到桌面后,再按桌面应用图标又重新打开该应用的解决方法
- airodump-ng wlan0mon doesn't work
- js第三方插件(消息 图形)
- Java接口和抽象类
- ZooKeeper【overview -welcome】
- NBAapi完全免费啦!
- Android 开源框架
- github中Android优秀案例
- 我的第一个jsp程序-实现注册登录留言功能
- 类加载器
- iOS开发 数据存取方法 对象归档
- STM32学习第六天---稍微了解了下下端口复用和重映像
- XListView
- JS 之 验证日期格式
- scons编译protobuffer脚本代码
- 简述如何配置JConsole以实现对ArcGIS for Server的监控
- 出现Error:java: javacTask: source release 1.7 requires target release 1.7
- HDU 5584 (数学)
- ECSHOP提交订单时提示“您的购物车中没有商品”
- Leetcode 第8题 String to Integer (atoi)