JAVA 获取文件的MD5值大小以及常见的工具类
生活随笔
收集整理的這篇文章主要介紹了
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
總結
以上是生活随笔為你收集整理的JAVA 获取文件的MD5值大小以及常见的工具类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转】Android Broadcast
- 下一篇: [转]iOS为UILabel添加长按复制