• JAVA 校验图片视频属性


    1. 需求: 文件上传的时候,需要根据图片或者视频校验尺寸比例和大小是否符合要求

    2. 代码

    2.1 前期准备

    2.1.1. 需要的jar包

    1. maven仓库没有这个jar包,需要从网上下载,导入本地
    链接:https://www.aliyundrive.com/s/enXJZnujTGf
    
    • 1
    1. 在项目根目录新建 lib 文件夹,把jar包放入该文件夹
      在这里插入图片描述

    2.1.2. maven配置

    导入依赖

    
    <dependency>
        <groupId>it.sauronsoftwaregroupId>
        <artifactId>javeartifactId>
        <version>1.0.2version>
        <scope>systemscope>
        <systemPath>/${project.basedir}/lib/jave-1.0.2.jarsystemPath>
    dependency>
    
    
    <dependency>
         <groupId>cn.hutoolgroupId>
         <artifactId>hutool-allartifactId>
         <version>5.5.8version>
     dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2.2 代码

    @Override
    public String uploadFile(MultipartFile multipartFile, String type) throws IOException {
        String result = null;
        String filename = multipartFile.getOriginalFilename();
    
    	//把文件流复制到本地  或者把MultipartFile  转file
        File file = new File(localDirectory + filename);
        FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);
        InputStream inputStream = null;
        Double height = 0.0;
        Double width = 0.0;
        long kbSize;
        long duration = 0;
    
        //  type  1: 图片  2:  icon  3=视频
        if ("1".equals(type)) {
            /*
            校验图片
            宽高比例:9:16、16:9;大小不超过300KB ; 格式:png、jpg、jpeg
             */      
            inputStream = new FileInputStream(file);
            BufferedImage image = ImageIO.read(inputStream);
            height = (double) image.getHeight();
            width = (double) image.getWidth();
            kbSize = cn.hutool.core.io.FileUtil.size(file) / 1024;
           
            if (!isImage(filename)) {
                result = "请上传格式为png、jpg、jpeg的图片!";         
            }
        } else if ("2".equals(type)) {
            /*
            校验icon
           200x200以内,大小不超过200KB,格式:png、jpg、jpeg
             */
           
           inputStream = new FileInputStream(file);
            BufferedImage image = ImageIO.read(inputStream);
            height = (double) image.getHeight();
            width = (double) image.getWidth();  
            kbSize = cn.hutool.core.io.FileUtil.size(file) / 1024;     
            if (!isImage(filename)) {
                result = "请上传格式为png、jpg、jpeg的图片!";         
            }
        } else {
            /*
            校验视频
            宽高比例:9:16、16:9,大小<=10M,5s<=时长格式<=30s,支持4g
             */
            Encoder encoder = new Encoder();  
            MultimediaInfo m = encoder.getInfo(file);
            height = (double) m.getVideo().getSize().getHeight();
            width = (double) m.getVideo().getSize().getWidth();
            //时长  秒
            duration = m.getDuration() / 1000;       
            kbSize = cn.hutool.core.io.FileUtil.size(file) / 1024 ;  
            if (!isVideo(filename)) {
                result = "请上传视频格式的文件!";      
            }
        }
    }
    
    
    private Boolean isImage(String fileName) {
         String reg = "(jpg|jpeg|png)";
         Pattern p = Pattern.compile(reg);
         return p.matcher(fileName).find();
     }
    private boolean isVideo(String fileName) {
        String reg = "(mp4|flv|avi|rm|rmvb|wmv)";
        Pattern p = Pattern.compile(reg);
        boolean boo = p.matcher(fileName).find();
        return boo;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
  • 相关阅读:
    Git的初步认识
    java计算机毕业设计个人网站设计源程序+mysql+系统+lw文档+远程调试
    ubuntu根目录清理
    《熬夜整理》保姆级系列教程-玩转Wireshark抓包神器教程(2)-Wireshark在Windows系统上安装部署
    在线教育市场持续火爆,潜力巨大
    Linux:TCP三握四挥简析
    div盒子放在页面正中间,添加旋转动画的时候,盒子向右下偏移
    java强应用,软引用,弱引用,虚引用的区别
    Haproxy负载均衡集群
    百万数据直接返回,高性能量化数据库Arctic,基于mongo的列存引擎
  • 原文地址:https://blog.csdn.net/abcd741258358/article/details/126779330