首页 > 编程语言 > 详细

Java工具类(文件)

时间:2020-07-08 00:46:31      阅读:94      评论:0      收藏:0      [点我收藏+]

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. 待续

 

Java工具类(文件)

原文:https://www.cnblogs.com/ruhuanxingyun/p/13264188.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!