JAVA 获取文件的MD5值大小以及常见的工具类

时间:2023-03-09 18:02:08
JAVA 获取文件的MD5值大小以及常见的工具类
 /**
* 获取文件的MD5值大小
*
* @param file
* 文件对象
* @return
*/
public static String getMD5(File file) {
FileInputStream fileInputStream = null;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
fileInputStream = new FileInputStream(file);
byte[] buffer = new byte[8192];
int length = 0;
while ((length = fileInputStream.read(buffer)) != -1) {
md5.update(buffer, 0, length);
}
return new String(Hex.encodeHex(md5.digest()));
} catch (Exception e) {
DEBUGGER.error("file{} MD5 fail", e);
return null;
} finally {
try {
if (null != fileInputStream) {
fileInputStream.close();
}
} catch (IOException e) {
DEBUGGER.error("close fileInputStream fail", e);
return null;
}
}
}

  

  常见的一些下载工具类:

  

 /**
*
* 下载文件
*
* @param files
* 文件列表
* @param file
* ZIP 压缩文件
* @param request
* 请求对象
* @param response
* 返回对象
* @return servletResponse
* @throws Exception
*/
public static HttpServletResponse downLoadFiles(List<CusFile> files, CusFile cusFile, HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
/**
* 这个集合就是你想要打包的所有文件, 这里假设已经准备好了所要打包的文件
*/
// List<File> files = new ArrayList<File>(); /**
* 创建一个临时压缩文件, 我们会把文件流全部注入到这个文件中 这里的文件你可以自定义是.rar还是.zip
*/
// File file = new File("c:/certpics.rar");
/*
* if (!file.exists()){ file.createNewFile(); }
*/
response.reset();
// response.getWriter()
// 创建文件输出流
FileOutputStream fous = new FileOutputStream(cusFile.getFile());
/**
* 打包的方法我们会用到ZipOutputStream这样一个输出流, 所以这里我们把输出流转换一下
*/
// org.apache.tools.zip.ZipOutputStream zipOut = new
// org.apache.tools.zip.ZipOutputStream(fous);
ZipOutputStream zipOut = new ZipOutputStream(fous);
zipFile(files, zipOut);
zipOut.close();
fous.close();
return downloadZip(cusFile, response);
} catch (Exception e) {
e.printStackTrace();
}
/**
* 直到文件的打包已经成功了, 文件的打包过程被我封装在FileUtil.zipFile这个静态方法中,
* 稍后会呈现出来,接下来的就是往客户端写数据了
*/
// OutputStream out = response.getOutputStream();
return response;
} public static HttpServletResponse downloadZip(CusFile cusFile, HttpServletResponse response) {
File file = cusFile.getFile();
try {
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset(); OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment; filename=" + new String(cusFile.getLogicFileName().getBytes("gbk"), "iso-8859-1"));
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
File f = new File(file.getPath());
f.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
} /**
* 把接受的全部文件打成压缩包
*
* @param List
* <File>;
* @param org
* .apache.tools.zip.ZipOutputStream
*/
public static void zipFile(List<CusFile> files, ZipOutputStream outputStream) {
int size = files.size();
for (int i = 0; i < size; i++) {
CusFile file = (CusFile) files.get(i);
zipFile(file, outputStream);
}
} /**
* 根据输入的文件与输出流对文件进行打包
*
* @param File
* @param org
* .apache.tools.zip.ZipOutputStream
*/
public static void zipFile(CusFile inputCusFile, ZipOutputStream ouputStream) {
try {
File inputFile = inputCusFile.getFile();
if (inputFile.exists()) {
/**
* 如果是目录的话这里是不采取操作的, 至于目录的打包正在研究中
*/
if (inputFile.isFile()) {
FileInputStream IN = new FileInputStream(inputFile);
BufferedInputStream bins = new BufferedInputStream(IN, 512);
// org.apache.tools.zip.ZipEntry
String entryName = new String(inputCusFile.getLogicFileName().getBytes(System.getProperty("file.encoding")), "utf-8");
ZipEntry entry = new ZipEntry(entryName);
ouputStream.putNextEntry(entry);
// 向压缩文件中输出数据
int nNumber;
byte[] buffer = new byte[512];
while ((nNumber = bins.read(buffer)) != -1) {
ouputStream.write(buffer, 0, nNumber);
}
// 关闭创建的流对象
bins.close();
IN.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

  

 public class CusFile {

     private String logicFileName;

     private File file;
// 省略getter and setter
}

  判断字符串或者对象的方法

  

 /**
* 校验对象是否为空
*
* @param object 传入对象
* @return true:空 null:非空
*/
public static boolean isNull(Object object) {
return (object == null);
} /**
* 校验对象是否不为空
*
* @param object 传入对象
* @return true:不为空 false:为空
*/
public static boolean isNotNull(Object object) {
return (!(isNull(object)));
} /**
* 校验集合对象是否为空
*
* @param coll 集合对象
* @return true:为空 false:不为空
*/
public static boolean isEmpty(Collection<?> coll) {
return ((isNull(coll)) || (coll.isEmpty()));
} /**
* 校验集合对象是否不为空
*
* @param coll 集合对象
* @return true:不为空 false:不为空
*/
public static boolean isNotEmpty(Collection<?> coll) {
return (!(isEmpty(coll)));
} /**
* 校验传入的字符串是否为空
*
* @param str 传入字符串
* @return true:为空 false:不为空
*/
public static boolean isEmpty(String str) {
return ((isNull(str)) || ("".equals(str.trim())));
} /**
* 校验传入的字符串是否不为空
*
* @param str 传入字符串
* @return true:不为空 false:为空
*/
public static boolean isNotEmpty(String str) {
return (!(isEmpty(str)));
} /**
* 校验数组对象是否为空
*
* @param objects 数组对象
* @return true:为空 false:不为空
*/
public static boolean isEmpty(Object[] objects) {
return ((isNull(objects)) || (objects.length == 0));
} /**
* 校验数组对象是否不为空
*
* @param objects 数组对象
* @return true:不为空 false:为空
*/
public static boolean isNotEmpty(Object[] objects) {
return (!(isEmpty(objects)));
} /**
* 校验MAP集合是否为空
*
* @param map map集合对象
* @return true:为空 false:不为空
*/
public static boolean isEmpty(Map<?, ?> map) {
return ((isNull(map)) || (map.isEmpty()));
} /**
* 校验MAP集合是否不为空
*
* @param map map集合对象
* @return true:不为空 false:为空
*/
public static boolean isNotEmpty(Map<?, ?> map) {
return (!(isEmpty(map)));
}

  

 /**
* 判断字符串是否是整数
*/
public static boolean isInteger(String value) {
try {
Integer.parseInt(value);
return true;
} catch (NumberFormatException e) {
return false;
}
} /**
* 判断字符串是否是浮点数
*/
public static boolean isDouble(String value) {
try {
Double.parseDouble(value);
if (value.contains("."))
return true;
return false;
} catch (NumberFormatException e) {
return false;
}
} /**
* 判断字符串是否是数字
*/
public static boolean isNumber(String value) {
return isInteger(value) || isDouble(value);
}

  

相关文章