2023年12月11日发(作者:)

java实现MD5文件校验

需求背景:简单实现远程文件的MD5校验

方案设计:①通过FTP获取远程文件流;②将文件流处理获取MD5;

已知bug:①中文路径乱码

的工具类

关于中文路径的乱码bug没能解决

import xception;

import cturedStorageReaderUtil;

import ;

import izerFeature;

import .*;

import y;

import s;

import Utils;

import ;

import Factory;

import rayOutputStream;

import tream;

import Stream;

import ;

import .*;

public class SftpHelper {

private static final Logger LOG = ger();

Session session = null;

ChannelSftp channelSftp = null;

public static Long totalCount = 0L;

public void loginFtpServer(String host, String username, String password, int port, int timeout,

String connectMode) {

JSch jsch = new JSch(); // 创建JSch对象

try {

session = sion(username, host, port);

// 根据用户名,主机ip,端口获取一个Session对象

// 如果服务器连接不上,则抛出异常

if (session == null) {

throw XException(_LOGIN,

"session is null,无法通过sftp与服务器建立链接,请检查主机名和用户名是否正确.");

}

sword(password); // 设置密码

Properties config = new Properties();

("StrictHostKeyChecking", "no");

fig(config); // 为Session对象设置properties

eout(timeout); // 设置timeout时间

t(); // 通过Session建立链接

channelSftp = (ChannelSftp) annel("sftp"); // 打开SFTP通道

t(); // 建立SFTP通道的连接

try {

Class cl = ss();

Field field = laredField("server_version");

essible(true);

(channelSftp,2);

enameEncoding("GBK");

} catch (SftpException e) {

tackTrace();

String message = ("-------------------------", host);

(message);

} catch (IllegalAccessException e) {

tackTrace();

} catch (NoSuchFieldException e) {

tackTrace();

}

//设置命令传输编码

//String fileEncoding = perty("ng");

//enameEncoding(fileEncoding);

} catch (JSchException e) {

if(null != se()){

String cause = se().toString();

String unknownHostException = "nHostException: " + host;

String illegalArgumentException = "lArgumentException: port out of range:" + port;

String wrongPort = "tException: Connection refused";

if ((cause)) {

String message = ("请确认ftp服务器地址是否正确,无法连接到地址为: [%s] 的ftp服务器", host);

(message);

throw XException(_LOGIN, message, e);

} else if ((cause) || (cause) ) {

String message = ("请确认连接ftp服务器端口是否正确,错误的端口: [%s] ", port);

(message);

throw XException(_LOGIN, message, e);

}

}else {

if("Auth fail".equals(sage())){

String message = ("与ftp服务器建立连接失败,请检查用户名和密码是否正确: [%s]",

"message:host =" + host + ",username = " + username + ",port =" + port);

(message);

throw XException(_LOGIN, message);

}else{

String message = ("与ftp服务器建立连接失败 : [%s]",

"message:host =" + host + ",username = " + username + ",port =" + port);

(message);

throw XException(_LOGIN, message, e);

}

}

}

}

public void logoutFtpServer() {

if (channelSftp != null) {

nect();

}

if (session != null) {

nect();

}

}

public void mkdir(String directoryPath) {

boolean isDirExist = false;

try {

orkingDirectory();

SftpATTRS sftpATTRS = (directoryPath);

isDirExist = ();

} catch (SftpException e) {

if (sage().toLowerCase().equals("no such file")) {

((

"您的配置项path:[%s]不存在,将尝试进行目录创建, errorMessage:%s",

directoryPath, sage()), e);

isDirExist = false;

}

}

if (!isDirExist) {

try {

// warn 检查mkdir -p

(directoryPath);

} catch (SftpException e) {

String message = String

.format("创建目录:%s时发生I/O异常,请确认与ftp服务器的连接正常,拥有目录创建权限, errorMessage:%s",

directoryPath, sage());

directoryPath, sage());

(message, e);

throw DataXException

.asDataXException(

D_FTP_IO_EXCEPTION,

message, e);

}

}

}

public void mkDirRecursive(String directoryPath){

boolean isDirExist = false;

try {

orkingDirectory();

SftpATTRS sftpATTRS = (directoryPath);

isDirExist = ();

} catch (SftpException e) {

if (sage().toLowerCase().equals("no such file")) {

(("您的配置项path:[%s]不存在,将尝试进行目录创建, errorMessage:%s",directoryPath, sage()), e);

isDirExist = false;

}

}

if (!isDirExist) {

StringBuilder dirPath = new StringBuilder();

(_SEPARATOR_UNIX);

String[] dirSplit = (directoryPath,_SEPARATOR_UNIX);

try {

// ftp server不支持递归创建目录,只能一级一级创建

for(String dirName : dirSplit){

try {

(dirName);

(ng());

(_SEPARATOR_UNIX);

} catch (SftpException e1) {

String message = ("没有此目录:%s,开始创建=======================================, errorMessage:%s",dirName, sage());

(message, e1);

(dirName);

(ng());

(_SEPARATOR_UNIX);

}

/*(dirName);

mkDirSingleHierarchy(ng());

(_SEPARATOR_UNIX);*/

}

} catch (SftpException e) {

String message = String

.format("创建目录:%s时发生I/O异常,请确认与ftp服务器的连接正常,拥有目录创建权限, errorMessage:%s",

directoryPath, sage());

(message, e);

throw DataXException

.asDataXException(

D_FTP_IO_EXCEPTION,

message, e);

}

}

}

public boolean mkDirSingleHierarchy(String directoryPath) throws SftpException {

boolean isDirExist = false;

try {

SftpATTRS sftpATTRS = (directoryPath);

isDirExist = ();

} catch (SftpException e) {

if(!isDirExist){

(("正在逐级创建目录 [%s]",directoryPath));

(directoryPath);

return true;

}

}

if(!isDirExist){

(("正在逐级创建目录 [%s]",directoryPath));

(directoryPath);

}

}

return true;

}

public OutputStream getOutputStream(String filePath) {

try {

orkingDirectory();

String parentDir = ing(0,dexOf(filePath, _SEPARATOR_UNIX));

(parentDir);

orkingDirectory();

OutputStream writeOutputStream = (filePath,);

if (null == writeOutputStream) {

String message = (

"打开FTP文件[%s]获取写出流时出错,请确认文件%s有权限创建,有权限写出等", filePath,filePath);

throw XException(_FILE_ERROR, message);

}

return writeOutputStream;

} catch (SftpException e) {

String message = (

"写出文件[%s] 时出错,请确认文件%s有权限写出, errorMessage:%s", filePath,filePath, sage());

(message);

throw XException(_FILE_ERROR, message);

}

}

public Long getOutputStream(InputStream in, String filePath) {

try {

orkingDirectory();

//断点续传

String parentDir = ing(0,dexOf(filePath, _SEPARATOR_UNIX));

(parentDir);

orkingDirectory();

(in, filePath, new MyProgressMonitor(), );

return totalCount;

} catch (SftpException e) {

String message = ("写出文件[%s] 时出错,请确认文件%s有权限写出, errorMessage:%s", filePath,filePath, sage());

(message);

throw XException(_FILE_ERROR, message);

}

}

public String getRemoteFileContent(String filePath) {

try {

tePendingCommand();

orkingDirectory();

String parentDir = ing(0,

dexOf(filePath, _SEPARATOR_UNIX));

(parentDir);

orkingDirectory();

ByteArrayOutputStream outputStream = new ByteArrayOutputStream(22);

(filePath, outputStream);

String result = ng();

uietly(outputStream);

return result;

} catch (SftpException e) {

String message = (

"写出文件[%s] 时出错,请确认文件%s有权限写出, errorMessage:%s", filePath,

filePath, sage());

(message);

throw XException(

_FILE_ERROR, message);

}

}

public Set getAllFilesInDir(String dir, String prefixFileName) {

Set allFilesWithPointedPrefix = new HashSet();

try {

orkingDirectory();

@SuppressWarnings("rawtypes")

Vector allFiles = (dir);

(("ls: %s", String(allFiles,

gleQuotes)));

for (int i = 0; i < (); i++) {

LsEntry le = (LsEntry) (i);

String strName = ename();

if (With(prefixFileName)) {

(strName);

}

}

} catch (SftpException e) {

String message = String

.format("获取path:[%s] 下文件列表时发生I/O异常,请确认与ftp服务器的连接正常,拥有目录ls权限, errorMessage:%s",

dir, sage());

(message);

throw XException(

D_FTP_IO_EXCEPTION, message, e);

}

return allFilesWithPointedPrefix;

}

public void deleteFiles(Set filesToDelete) {

String eachFile = null;

try {

orkingDirectory();

for (String each : filesToDelete) {

(("delete file [%s].", each));

eachFile = each;

(each);

}

} catch (SftpException e) {

String message = (

"删除文件:[%s] 时发生异常,请确认指定文件有删除权限,以及网络交互正常, errorMessage:%s",

eachFile, sage());

(message);

throw XException(

D_FTP_IO_EXCEPTION, message, e);

}

}

private void printWorkingDirectory() {

try {

(("current working directory:%s",()));

} catch (Exception e) {

(("printWorkingDirectory error:%s",sage()));

}

}

public void completePendingCommand() {

}

public boolean isDirExist(String directoryPath) {

try {

SftpATTRS sftpATTRS = (directoryPath);

return ();

} catch (SftpException e) {

return false;

}

}

public boolean isFileExist(String filePath) {

boolean isExitFlag = false;

try {

SftpATTRS sftpATTRS = (filePath);

if(e() >= 0){

isExitFlag = true;

}

} catch (SftpException e) {

} catch (SftpException e) {

if (sage().toLowerCase().equals("no such file")) {

String message = ("请确认您的配置项path:[%s]存在,且配置的用户有权限读取", filePath);

(message);

throw XException(_NOT_EXISTS, message);

} else {

String message = ("获取文件:[%s] 属性时发生I/O异常,请确认与ftp服务器的连接正常", filePath);

(message);

throw XException(D_FTP_IO_EXCEPTION, message, e);

}

}

return isExitFlag;

}

public boolean isSymbolicLink(String filePath) {

try {

SftpATTRS sftpATTRS = (filePath);

return ();

} catch (SftpException e) {

if (sage().toLowerCase().equals("no such file")) {

String message = ("请确认您的配置项path:[%s]存在,且配置的用户有权限读取", filePath);

(message);

throw XException(_NOT_EXISTS, message);

} else {

String message = ("获取文件:[%s] 属性时发生I/O异常,请确认与ftp服务器的连接正常", filePath);

(message);

throw XException(D_FTP_IO_EXCEPTION, message, e);

}

}

}

HashSet sourceFiles = new HashSet();

public HashSet getListFiles(String directoryPath, int parentLevel, int maxTraversalLevel ,String fileSuffix) {

if(parentLevel < maxTraversalLevel){

String parentPath = null;// 父级目录,以'/'结尾

int pathLen = ();

if (ns("*") || ns("?")) {//*和?的限制

// path是正则表达式

String subPath = exPathParentPath(directoryPath);

if (isDirExist(subPath)) {

parentPath = subPath;

} else {

String message = ("不能进入目录:[%s]," + "请确认您的配置项path:[%s]存在,且配置的用户有权限进入", subPath,

directoryPath);

(message);

throw XException(_NOT_EXISTS, message);

}

} else if (isDirExist(directoryPath)) {

// path是目录

if ((pathLen - 1) == _SEPARATOR_UNIX) {

parentPath = directoryPath;

} else {

parentPath = directoryPath + _SEPARATOR_UNIX;

}

} else if(isSymbolicLink(directoryPath)){

//path是链接文件

String message = ("文件:[%s]是链接文件,当前不支持链接文件的读取", directoryPath);

(message);

throw XException(_FILE, message);

}else if (isFileExist(directoryPath)) {

// path指向具体文件

(directoryPath);

return sourceFiles;

} else {

String message = ("请确认您的配置项path:[%s]存在,且配置的用户有权限读取", directoryPath);

(message);

throw XException(_NOT_EXISTS, message);

}

try {

try {

Vector vector = (directoryPath);

for (int i = 0; i < (); i++) {

LsEntry le = (LsEntry) (i);

String strName = ename();

String filePath = parentPath + strName;

if (isDirExist(filePath)) {

// 是子目录

if (!((".") || (".."))) {

//递归处理

getListFiles(filePath, parentLevel+1, maxTraversalLevel, fileSuffix);

}

} else if(isSymbolicLink(filePath)){

//是链接文件

String message = ("文件:[%s]是链接文件,当前不支持链接文件的读取", filePath);

(message);

throw XException(_FILE, message);

}else if (isFileExist(filePath)) {

// 是文件

//是否有要求的 后缀

if(fileSuffix != null && !("")){

List list = ((","));

().forEach(l ->{

if(ns(l)) (filePath);

});

}else {

(filePath);

}

} else {

String message = ("请确认path:[%s]存在,且配置的用户有权限读取", filePath);

(message);

throw XException(_NOT_EXISTS, message);

}

} // end for vector

} catch (SftpException e) {

String message = ("获取path:[%s] 下文件列表时发生I/O异常,请确认与ftp服务器的连接正常", directoryPath);

(message);

throw XException(D_FTP_IO_EXCEPTION, message, e);

}

return sourceFiles;

}else{

//超出最大递归层数

String message = ("获取path:[%s] 下文件列表时超出最大层数,请确认路径[%s]下不存在软连接文件", directoryPath, directoryPath);

(message);

throw XException(_MAX_DIRECTORY_LEVEL, message);

}

}

public InputStream getInputStream(String filePath) {

try {

return (filePath);

} catch (SftpException e) {

String message = ("读取文件 : [%s] 时出错,请确认文件:[%s]存在且配置的用户有权限读取", filePath, filePath);

(message);

throw XException(_FILE_ERROR, message);

}

}

public Date lstat(String filePath) {

try {

SftpATTRS attrs = (filePath);

Date lastModified = new Date(me() * 1000L);

return lastModified;

}catch (Exception e){

("获取文件"+filePath+"最新修改时间有误:"+ng());

}

return null;

}

}

5的工具类

其中关于字符串的MD5校验功能可以用来解决文件夹的MD5校验

/**

* @ClassName:Md5Utils

* @author:莫须有 (来自网络)

* @Description: 生成文件的MD5校验码

* @create:2020/11/5 11:31

* @Version1.0

*/

import Utils;

import ;

import putStream;

import ption;

import tream;

public class Md5Utils {

/**

* 生成字符串的md5校验值

*

* @param s

* @return

*/

public static String getMD5String(String s) {

return 5Hex(s);

}

/**

* 判断字符串的md5校验码是否与一个已知的md5码相匹配

*

* @param md5str1 要校验的字符串

* @param md5str2 已知的md5校验码

* @return

*/

public static boolean checkMd5(String md5str1, String md5str2) {

return (md5str2);

}

/**

* 生成文件的md5校验值

*

* @param file

* @return

* @throws IOException

*/

public static String getFileMD5String(File file) throws IOException {

InputStream fis;

fis = new FileInputStream(file);

String md5=5Hex(fis);

();

return md5;

}

public static String getFileInputStreamMD5String(InputStream fis) throws IOException {

String md5=5Hex(fis);

();

return md5;

}

}