1. 工具类jar包

  • 这里使用gradle,不是maven
  • 检查文件类型的工具包,文档链接
implementation 'cn.hutool:hutool-all:5.7.12'

2. 配置文件

# 文件大小
spring.servlet.multipart.max-file-size=5MB
# 请求大小
spring.servlet.multipart.max-request-size=10MB

3. 检查文件类型

  • 如果工具类中不存在你需要限制的文件类型(比如苹果手机的图片格式heic),则需要提前检查出文件的字节头(使用bytesToHexString方法),再添加到工具类的Map中
package com.example.fisher.gradledemo.controller;

import cn.hutool.core.io.FileTypeUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.Image;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.Set;

@RestController
public class CheckFileType {

    private final String imageType = "image";

    @PostMapping("/checkFileType")
    public boolean checkFileType(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        // 打印文件字节头
        System.out.println(bytesToHexString(multipartFile.getBytes()).substring(0, 20));
        // 限制的文件类型集合
        Set<String> fileType = Set.of("jpg", "png", "heic");
        // 将自定义的文件类型字节头添加到工具类的Map中
        FileTypeUtil.putFileType("000000286674", "heic");
        // 检查文件类型
        String type = FileTypeUtil.getType(multipartFile.getInputStream());
        System.out.println(type);
        // 判断是否在限制的文件类型集合中,如果为null,直接返回false
        if (!Optional.ofNullable(type).map(fileType::contains).orElse(false)) {
            return false;
        }
        // 判断content type是不是image
        if (!multipartFile.getContentType().contains(imageType)) {
            return false;
        }
        // 判断图片尺寸
        if (!isImage(multipartFile.getInputStream())) {
            return false;
        }
        return true;
    }

    /**
     * 检查文件字节头
     */
    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    /**
     * 通过图片尺寸,判断宽和高是否大于0,校验是否为图片
     */
    private boolean isImage(InputStream in) throws IOException {
        try {
            Image img = ImageIO.read(in);
            return !(img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0);
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }

}

在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐