校验base64字符串是否为jpg图片

import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Scanner;

/**
 * @Author: dell
 * @Version: V1.0.0
 * @Date: 2021年05月28日
 */
public class Base64SCheckJPG {

    // 影印件格式
    private static final String JPG = "FFD8FFE0";
    private static final String JPG1 = "FFD8FFE1";
    private static final String JPG2 = "FFD8FFE8";
    // 影印件大小
    private static final int IMAGE_SIZE_MAX = 500 * 1024;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String base64Message= sc.next();
        String responseCode = "2000";
        try {
            responseCode =  checkImage(base64Message);
        } catch (Base64DecodingException e) {
            e.printStackTrace();
        }
        switch (responseCode){
            case "2001":
                System.out.println(responseCode + ":参数不正确:影印件不是图片格式。");
                break;
            case "2002":
                System.out.println(responseCode + ":参数不正确:影印件不是JPG格式。");
                break;
            case "2003":
                System.out.println(responseCode + ":参数不正确:影印件大小超限。");
                break;
            default:
                System.out.println(responseCode + ":OK.");
        }

    }

    public static String checkImage(String imgDataStr) throws Base64DecodingException {
        // 将 data:image/jpg;base64, 去掉
        int i = imgDataStr.indexOf(",");
        imgDataStr = imgDataStr.substring(i+1);

        // 图片内容 base64 解密。
        byte[] data = Base64.decode(imgDataStr);

        String code = "2000";

        // 校验是否是图片格式
        InputStream inputStream = new ByteArrayInputStream(data);
        if (!isImage(inputStream)) {
            code = "2001";
            return code;
        }
        // 校验是否是jpg格式
        if (!checkFileType(data)) {
            code = "2002";
            return code;
        }
        // 校验图片大小
        if (data.length > IMAGE_SIZE_MAX) {
            code = "2003";
            return code;
        }
        return code;
    }

    /**
     * 校验是否是图片:如果能够正常的获取到宽高属性,那么这是一张图片,因为非图片文件是获取不到它的宽高属性的
     * @param inputStream
     * @return
     */
    private static boolean isImage(InputStream inputStream) {
        Image img;
        try {
            img = ImageIO.read(inputStream);
            return !(img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 校验图片格式:根据图片的前面几个字节来判断
     * @param data
     * @return
     */
    private static boolean checkFileType(byte[] data) {
        try {
            StringBuilder sb = new StringBuilder();
            // 文件头占4位。
            if (data.length < 4) {
                return false;
            } else {
                // 读取文件头。
                for (int j = 0; j < 4; j++) {
                    int v = data[j] & 0xFF;
                    String hv = Integer.toHexString(v);
                    if (hv.length() < 2) {
                        sb.append(0);
                    }
                    sb.append(hv);
                }
            }
            System.out.println("文件头四位:" + sb.toString());
            return JPG.equalsIgnoreCase(sb.toString()) || JPG1.equalsIgnoreCase(sb.toString())
                    || JPG2.equalsIgnoreCase(sb.toString());
        } catch (Exception e) {
            return false;
        }
    }
}

 

上一篇:java版直播商城之直播间代码介绍Spring Cloud+SpringBoot+mybatis+uniapp b2b2c微服务商城


下一篇:java版Spring Cloud+Spring Boot+mybatis+security+uniapp+Redis+MQ+VR全景+直播短视频带货+三级分销+拼团商城 +分销商城