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

 1 /**
 2      * 获取文件的MD5值大小
 3      * 
 4      * @param file
 5      *            文件对象
 6      * @return
 7      */
 8     public static String getMD5(File file) {
 9         FileInputStream fileInputStream = null;
10         try {
11             MessageDigest md5 = MessageDigest.getInstance("MD5");
12             fileInputStream = new FileInputStream(file);
13             byte[] buffer = new byte[8192];
14             int length = 0;
15             while ((length = fileInputStream.read(buffer)) != -1) {
16                 md5.update(buffer, 0, length);
17             }
18             return new String(Hex.encodeHex(md5.digest()));
19         } catch (Exception e) {
20             DEBUGGER.error("file{} MD5 fail", e);
21             return null;
22         } finally {
23             try {
24                 if (null != fileInputStream) {
25                     fileInputStream.close();
26                 }
27             } catch (IOException e) {
28                 DEBUGGER.error("close fileInputStream fail", e);
29                 return null;
30             }
31         }
32     }

  

  常见的一些下载工具类:

  

  1 /**
  2      * 
  3      * 下载文件
  4      * 
  5      * @param files
  6      *            文件列表
  7      * @param file
  8      *            ZIP 压缩文件
  9      * @param request
 10      *            请求对象
 11      * @param response
 12      *            返回对象
 13      * @return servletResponse
 14      * @throws Exception
 15      */
 16     public static HttpServletResponse downLoadFiles(List<CusFile> files, CusFile cusFile, HttpServletRequest request, HttpServletResponse response)
 17             throws Exception {
 18         try {
 19             /**
 20              * 这个集合就是你想要打包的所有文件, 这里假设已经准备好了所要打包的文件
 21              */
 22             // List<File> files = new ArrayList<File>();
 23 
 24             /**
 25              * 创建一个临时压缩文件, 我们会把文件流全部注入到这个文件中 这里的文件你可以自定义是.rar还是.zip
 26              */
 27             // File file = new File("c:/certpics.rar");
 28             /*
 29              * if (!file.exists()){ file.createNewFile(); }
 30              */
 31             response.reset();
 32             // response.getWriter()
 33             // 创建文件输出流
 34             FileOutputStream fous = new FileOutputStream(cusFile.getFile());
 35             /**
 36              * 打包的方法我们会用到ZipOutputStream这样一个输出流, 所以这里我们把输出流转换一下
 37              */
 38             // org.apache.tools.zip.ZipOutputStream zipOut = new
 39             // org.apache.tools.zip.ZipOutputStream(fous);
 40             ZipOutputStream zipOut = new ZipOutputStream(fous);
 41             zipFile(files, zipOut);
 42             zipOut.close();
 43             fous.close();
 44             return downloadZip(cusFile, response);
 45         } catch (Exception e) {
 46             e.printStackTrace();
 47         }
 48         /**
 49          * 直到文件的打包已经成功了, 文件的打包过程被我封装在FileUtil.zipFile这个静态方法中,
 50          * 稍后会呈现出来,接下来的就是往客户端写数据了
 51          */
 52         // OutputStream out = response.getOutputStream();
 53         return response;
 54     }
 55 
 56     public static HttpServletResponse downloadZip(CusFile cusFile, HttpServletResponse response) {
 57         File file = cusFile.getFile();
 58         try {
 59             // 以流的形式下载文件。
 60             InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
 61             byte[] buffer = new byte[fis.available()];
 62             fis.read(buffer);
 63             fis.close();
 64             // 清空response
 65             response.reset();
 66 
 67             OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
 68             response.setContentType("application/octet-stream");
 69             response.setHeader("Content-disposition", "attachment; filename=" + new String(cusFile.getLogicFileName().getBytes("gbk"), "iso-8859-1"));
 70             toClient.write(buffer);
 71             toClient.flush();
 72             toClient.close();
 73         } catch (IOException ex) {
 74             ex.printStackTrace();
 75         } finally {
 76             try {
 77                 File f = new File(file.getPath());
 78                 f.delete();
 79             } catch (Exception e) {
 80                 e.printStackTrace();
 81             }
 82         }
 83         return response;
 84     }
 85 
 86     /**
 87      * 把接受的全部文件打成压缩包
 88      * 
 89      * @param List
 90      *            <File>;
 91      * @param org
 92      *            .apache.tools.zip.ZipOutputStream
 93      */
 94     public static void zipFile(List<CusFile> files, ZipOutputStream outputStream) {
 95         int size = files.size();
 96         for (int i = 0; i < size; i++) {
 97             CusFile file = (CusFile) files.get(i);
 98             zipFile(file, outputStream);
 99         }
100     }
101 
102     /**
103      * 根据输入的文件与输出流对文件进行打包
104      * 
105      * @param File
106      * @param org
107      *            .apache.tools.zip.ZipOutputStream
108      */
109     public static void zipFile(CusFile inputCusFile, ZipOutputStream ouputStream) {
110         try {
111             File inputFile = inputCusFile.getFile();
112             if (inputFile.exists()) {
113                 /**
114                  * 如果是目录的话这里是不采取操作的, 至于目录的打包正在研究中
115                  */
116                 if (inputFile.isFile()) {
117                     FileInputStream IN = new FileInputStream(inputFile);
118                     BufferedInputStream bins = new BufferedInputStream(IN, 512);
119                     // org.apache.tools.zip.ZipEntry
120                     String entryName = new String(inputCusFile.getLogicFileName().getBytes(System.getProperty("file.encoding")), "utf-8");
121                     ZipEntry entry = new ZipEntry(entryName);
122                     ouputStream.putNextEntry(entry);
123                     // 向压缩文件中输出数据
124                     int nNumber;
125                     byte[] buffer = new byte[512];
126                     while ((nNumber = bins.read(buffer)) != -1) {
127                         ouputStream.write(buffer, 0, nNumber);
128                     }
129                     // 关闭创建的流对象
130                     bins.close();
131                     IN.close();
132                 }
133             }
134         } catch (Exception e) {
135             e.printStackTrace();
136         }
137     }

  

1 public class CusFile {
2 
3     private String logicFileName;
4     
5     private File file;
6         // 省略getter and setter  
7 }

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

  

 1 /**
 2      * 校验对象是否为空
 3      * 
 4      * @param object 传入对象
 5      * @return true:空 null:非空
 6      */
 7     public static boolean isNull(Object object) {
 8         return (object == null);
 9     }
10     
11     /**
12      * 校验对象是否不为空
13      * 
14      * @param object 传入对象
15      * @return true:不为空 false:为空
16      */
17     public static boolean isNotNull(Object object) {
18         return (!(isNull(object)));
19     }
20     
21     /**
22      * 校验集合对象是否为空
23      * 
24      * @param coll 集合对象
25      * @return true:为空 false:不为空
26      */
27     public static boolean isEmpty(Collection<?> coll) {
28         return ((isNull(coll)) || (coll.isEmpty()));
29     }
30     
31     /**
32      * 校验集合对象是否不为空
33      * 
34      * @param coll 集合对象
35      * @return true:不为空 false:不为空
36      */
37     public static boolean isNotEmpty(Collection<?> coll) {
38         return (!(isEmpty(coll)));
39     }
40     
41     /**
42      * 校验传入的字符串是否为空
43      * 
44      * @param str 传入字符串
45      * @return true:为空 false:不为空
46      */
47     public static boolean isEmpty(String str) {
48         return ((isNull(str)) || ("".equals(str.trim())));
49     }
50 
51     /**
52      * 校验传入的字符串是否不为空
53      * 
54      * @param str 传入字符串
55      * @return true:不为空  false:为空
56      */
57     public static boolean isNotEmpty(String str) {
58         return (!(isEmpty(str)));
59     }
60     
61     /**
62      * 校验数组对象是否为空
63      * 
64      * @param objects 数组对象
65      * @return true:为空 false:不为空
66      */
67     public static boolean isEmpty(Object[] objects) {
68         return ((isNull(objects)) || (objects.length == 0));
69     }
70 
71     /**
72      * 校验数组对象是否不为空
73      * 
74      * @param objects 数组对象
75      * @return true:不为空 false:为空
76      */
77     public static boolean isNotEmpty(Object[] objects) {
78         return (!(isEmpty(objects)));
79     }
80     
81     /**
82      * 校验MAP集合是否为空
83      * 
84      * @param map map集合对象
85      * @return true:为空 false:不为空
86      */
87     public static boolean isEmpty(Map<?, ?> map) {
88         return ((isNull(map)) || (map.isEmpty()));
89     }
90     
91     /**
92      * 校验MAP集合是否不为空
93      * 
94      * @param map map集合对象
95      * @return true:不为空 false:为空
96      */
97     public static boolean isNotEmpty(Map<?, ?> map) {
98         return (!(isEmpty(map)));
99     }

   

 1 /** 
 2       * 判断字符串是否是整数 
 3       */  
 4      public static boolean isInteger(String value) {  
 5          try {  
 6              Integer.parseInt(value);  
 7              return true;  
 8          } catch (NumberFormatException e) {  
 9              return false;  
10          }  
11      }  
12       
13      /** 
14       * 判断字符串是否是浮点数 
15       */  
16      public static boolean isDouble(String value) {  
17          try {  
18              Double.parseDouble(value);  
19              if (value.contains("."))  
20                  return true;  
21              return false;  
22          } catch (NumberFormatException e) {  
23              return false;  
24          }  
25      }  
26       
27      /** 
28       * 判断字符串是否是数字 
29       */  
30      public static boolean isNumber(String value) {  
31          return isInteger(value) || isDouble(value);  
32      }   

  

原文地址:https://www.cnblogs.com/XQiu/p/5282243.html