1. 文件相关
package com.ruhuanxingyun.javabasic.util; import org.springframework.web.multipart.MultipartFile; import java.math.BigInteger; import java.security.MessageDigest; import java.util.Arrays; /** * @description: 文件工具类 * @author: ruphie * @date: Create in 2020/7/7 23:07 * @company: ruhuanxingyun */ public class FileUtils { private static final int SPLIT_SIZE = 10 * 1024 * 1024; /** * 获取文件MD5值 * * @param file 文件 * @return MD5 */ public static String getFileMd5(MultipartFile file) { try { byte[] bytes = file.getBytes(); MessageDigest messageDigest = MessageDigest.getInstance("MD5"); byte[] digest = messageDigest.digest(bytes); return new BigInteger(1, digest).toString(16); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 文件字节分割 * * @param bytes 字节 * @return 字节数组 */ public static byte[][] splitBytes(byte[] bytes) { int bytesLen = bytes.length; int arrayLen = (int) Math.ceil(bytesLen / SPLIT_SIZE); byte[][] bytesArr = new byte[arrayLen][]; int from, to; for (int i = 0; i < arrayLen; i++) { from = i * SPLIT_SIZE; to = from + SPLIT_SIZE; if (to > bytesLen) { to = bytesLen; } bytesArr[i] = Arrays.copyOfRange(bytes, from, to); } return bytesArr; } }
2. 待续
原文:https://www.cnblogs.com/ruhuanxingyun/p/13264188.html