Md5Utils增加获取文件MD5值,支持获取文件指定前部分的MD5。
This commit is contained in:
@@ -3,12 +3,16 @@
|
|||||||
*/
|
*/
|
||||||
package com.jeesite.common.codec;
|
package com.jeesite.common.codec;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import com.jeesite.common.io.IOUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MD5不可逆加密工具类
|
* MD5不可逆加密工具类
|
||||||
* @author ThinkGem
|
* @author ThinkGem
|
||||||
@@ -17,7 +21,6 @@ public class Md5Utils {
|
|||||||
|
|
||||||
private static final String MD5 = "MD5";
|
private static final String MD5 = "MD5";
|
||||||
private static final String DEFAULT_ENCODING = "UTF-8";
|
private static final String DEFAULT_ENCODING = "UTF-8";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对输入字符串进行md5散列.
|
* 对输入字符串进行md5散列.
|
||||||
@@ -63,5 +66,36 @@ public class Md5Utils {
|
|||||||
public static byte[] md5(InputStream input) throws IOException {
|
public static byte[] md5(InputStream input) throws IOException {
|
||||||
return DigestUtils.digest(input, MD5);
|
return DigestUtils.digest(input, MD5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件的MD5值
|
||||||
|
*/
|
||||||
|
public static String md5File(File file) {
|
||||||
|
return md5File(file, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件的MD5值,支持获取文件部分的MD5值
|
||||||
|
*/
|
||||||
|
public static String md5File(File file, int size) {
|
||||||
|
if (file != null && file.exists()){
|
||||||
|
InputStream in = null;
|
||||||
|
try {
|
||||||
|
byte[] bytes = null;
|
||||||
|
in = FileUtils.openInputStream(file);
|
||||||
|
if (size != -1 && file.length() >= size){
|
||||||
|
bytes = IOUtils.toByteArray(in, size);
|
||||||
|
}else{
|
||||||
|
bytes = IOUtils.toByteArray(in);
|
||||||
|
}
|
||||||
|
return EncodeUtils.encodeHex(md5(bytes));
|
||||||
|
} catch (IOException e) {
|
||||||
|
return StringUtils.EMPTY;
|
||||||
|
} finally {
|
||||||
|
IOUtils.closeQuietly(in);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return StringUtils.EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user