/**
* 获取文件的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);
}

  

JAVA 获取文件的MD5值大小以及常见的工具类的更多相关文章

  1. java获取文件的md5值

    import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...

  2. swift 获取文件的Md5值

    获取文件的Md5值的方法如下 func md5File(url: URL) -> String? { let bufferSize = 1024 * 1024 do { //打开文件 let f ...

  3. iOS开发之获取文件的md5值

    我们经常有下载文件上的需求 为了安全我们经常需要对文件进行md5校验 那我就来给大家分享一个很方便的获取文件md5值得方法. 首先需要引用系统库文件 #include <CommonCrypto ...

  4. Android获取文件的MD5值

    package my.bag; import java.io.File; import java.io.FileInputStream; import java.math.BigInteger; im ...

  5. 在浏览器端获取文件的MD5值

    前几天接到一个奇怪的需求,要在web页面中计算文件的md5值,还好这个项目是只需兼容现代浏览器的,不然要坑死了. 其实对文件进行md5,对于后端来说是及其简单的.比如使用Node.js,只要下面几行代 ...

  6. 就这样获取文件的MD5和大小

    纠结真蛋疼 判断一件事值不值得去做的唯一标准是这件事是不是令我纠结.如果纠结了,就不去做了!但是,人总要活着,又能怎样.谁说男人就没有那么几天...... 从极速妙传说起 在现在各大厂商都推出免费云盘 ...

  7. ios获取文件的MD5值

    一般我们在使用http或者socket上传或者下载文件的时候,经常会在完成之后经行一次MD5值得校验(尤其是在断点续传的时候用的更 多),校验MD5值是为了防止在传输的过程当中丢包或者数据包被篡改,在 ...

  8. Java,如何获取文件的MD5值

    MessageDigest类封装得很不错,简单易用 不多说,直接上代码 import java.io.FileInputStream;import java.security.MessageDiges ...

  9. java 中,如何获取文件的MD5值呢?如何比较两个文件是否完全相同呢?

    /** * Get MD5 of one file:hex string,test OK! * * @param file * @return */ public static String getF ...

随机推荐

  1. bzoj 1854 构图 并查集

    我们可以把一件装备看成一条边,两个属性看成两个点,那么这就相当于读入了一张图 当读入每一个x,y时,我们找到两个点的祖先节点,fx,fy,我们保证祖先节点在该连通块 中编号(装备属性)最大,用flag ...

  2. react native遇到的坑

    1.模拟器报错no bundle url present https://github.com/facebook/react-native/issues/12754 http://www.cnblog ...

  3. springboot读取配置文件的顺序

    前言 今天测试一些东西,发现配置文件连接的数据库一直不正常,数据也不对,今天请教了之后,原来springboot的配置文件加载不仅仅是项目内的配置文件. 正文 项目目录是这样的:文件夹下有:项目,ap ...

  4. zoj 1828 Fibonacci Numbers

    A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the firs ...

  5. PTA编程总

    7-1 币值转换 (20 分) 输入一个整数(位数不超过9位)代表一个人民币值(单位为元),请转换成财务要求的大写中文格式.如23108元,转换后变成“贰万叁仟壹百零捌”元.为了简化输出,用小写英文字 ...

  6. 阻塞和非阻塞I/O

    阻塞和非阻塞I/O是设备访问的两种不同模式,驱动程序可以灵活的支持用户空间对设备的这两种访问形式.        阻塞操作是指在执行设备操作时,若不能获得资源,则挂起进程直到满足可操作的条件后在进行操 ...

  7. streamsets stream selector 使用

    stream selector 就是一个选择器,可以方便的对于不同record 的数据进行区分,并执行不同的处理 pipeline flow stream selector 配置 local fs 配 ...

  8. Django 中间件使用

     前戏 我们在前面的课程中已经学会了给视图函数加装饰器来判断是用户是否登录,把没有登录的用户请求跳转到登录页面.我们通过给几个特定视图函数加装饰器实现了这个需求.但是以后添加的视图函数可能也需要加上装 ...

  9. Spring 装配Bean入门级

    装配解释: 创建应用对象之间协作关系的的行为通常称为装配(wiring),这也是依赖注入的本质 依赖注入是Spring的基础要素 一 : 使用spring装配Bean基础介绍 1 :声明Bean  B ...

  10. Python yield详解***

    yield的英文单词意思是生产,有时候感到非常困惑,一直没弄明白yield的用法. 只是粗略的知道yield可以用来为一个函数返回值塞数据,比如下面的例子: def addlist(alist): f ...