java上传文件 校验文件后缀名、文件大小、文件名字

代码

在需要上传的接口controller中调用

	@PostMapping("/upload")
    public xxx upload(MultipartFile file) {
    	// 校验文件大小、名字、后缀
        CommentMethod.uploadVerify(file);
		你的业务代码
    }

  

  package com.xxx.xxx.common;

    import org.springframework.web.multipart.MultipartFile;

    import java.util.Arrays;
    import java.util.Locale;

    /**
     * 公共方法
     */
    public class CommentMethod {

        /**
         * 文件后缀 支持的类型 todo 自行舍取
         */
        private static final String[] FILE_SUFFIX_SUPPORT = {".xlsx", ".xls", ".doc", ".docx", ".txt", ".csv",
                ".jpg", ".jpeg", ".png"};
        /**
         * 文件名字 需要排除的字符 todo 自行舍取
         */
        private static final String[] FILE_NAME_EXCLUDE = {
                "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "=", "_", "+",
                "~", "·", "!", "¥", "……", "(", ")", "——",
                "?", ",", "<", ">", ":", ";", "[", "]", "{", "}", "/", "\\", "|",
                "?", ",", "。", "《", "》", ":", ";", "【", "】", "、",
        };

        /**
         * 文件大小 10MB todo 自行更改
         */
        private static final long FILE_SIZE = 10 * 1024 * 1024;

        /**
         * 上传文件校验大小、名字、后缀
         *
         * @param multipartFile multipartFile
         */
        public static void uploadVerify(MultipartFile multipartFile) {
            // 校验文件是否为空
            if (multipartFile == null) {
                throw new RuntimeException("文件不能为空!");
            }

            // 校验文件大小
            long size = multipartFile.getSize();
            if (size > FILE_SIZE) {
                throw new RuntimeException("文件大小不能超过10MB!");
            }

            // 校验文件名字
            String originalFilename = multipartFile.getOriginalFilename();
            if (originalFilename == null) {
                throw new RuntimeException("文件名字不能为空!");
            }
            boolean nameFlag = false;
            for (String s : FILE_NAME_EXCLUDE) {
                if (originalFilename.contains(s)) {
                    nameFlag = true;
                    break;
                }
            }
            if (nameFlag) {
                throw new RuntimeException("文件名字不允许出现" + Arrays.toString(FILE_NAME_EXCLUDE) + "关键字!");
            }

            //后缀 类型 二选一 或者 都校验
            // 校验文件后缀
            if (!originalFilename.contains(".")) {
                throw new RuntimeException("文件不能没有后缀!");
            }
            String suffix = originalFilename.substring(originalFilename.lastIndexOf('.'));
            // 获取文件类型
            String fileType = multipartFile.getContentType();
            if( null == fileType ){
                throw new RuntimeException("获取不到文件类型!");
            }

            boolean flag = true;
            for (String s : FILE_SUFFIX_SUPPORT) {
                if (s.equals(suffix.toLowerCase(Locale.ROOT)) && s.equals(fileType.toLowerCase(Locale.ROOT))) {
                    flag = false;
                    break;
                }
            }

            if (flag) {
                throw new RuntimeException("文件格式仅限于" + Arrays.toString(FILE_SUFFIX_SUPPORT) + "!");
            }
        }
    }

    

posted @ 2023-10-09 14:57  Ash灬  阅读(1209)  评论(0)    收藏  举报