上传图片验证相关

private boolean checkImageFile(MultipartFile uploadImg,ShowImgInfoDto imgInfo) throws IOException {


        String fileName = uploadImg.getOriginalFilename();
        String extUpp =StringUtil.toUpperCase(fileName.substring(fileName.lastIndexOf(".") + 1));


        //根据扩展名判断是否为要求的图片

if (!extUpp.matches("^[(JPG)|(PNG)|(GIF)]+$")) {
            imgInfo.setResult("error");
            imgInfo.setMessage("请上传PNG、JPG、GIF格式的文件!");
            return false;
        }
        //根据图片内容判断是否为图片文件
        InputStream inputStream = uploadImg.getInputStream();
        BufferedImage bi = ImageIO.read(inputStream);
        if(bi == null){
            imgInfo.setResult("error");
            imgInfo.setMessage("此文件不为图片文件");
            return false;
        }

//限制图片的大小
        if (uploadImg.getSize() > 0.512 * 1024 * 1024) {
            imgInfo.setResult("error");
            imgInfo.setMessage("请上传512kb以内的文件!");
            return false;
        }
        return true;
    }

private boolean checkImageFile(MultipartFile uploadImg,ShowImgInfoDto imgInfo) throws IOException {


        String fileName = uploadImg.getOriginalFilename();
        String extUpp =StringUtil.toUpperCase(fileName.substring(fileName.lastIndexOf(".") + 1));


        //根据扩展名判断是否为要求的图片

if (!extUpp.matches("^[(JPG)|(PNG)|(GIF)]+$")) {
            imgInfo.setResult("error");
            imgInfo.setMessage("请上传PNG、JPG、GIF格式的文件!");
            return false;
        }
        //根据图片内容判断是否为图片文件
        InputStream inputStream = uploadImg.getInputStream();
        BufferedImage bi = ImageIO.read(inputStream);
        if(bi == null){
            imgInfo.setResult("error");
            imgInfo.setMessage("此文件不为图片文件");
            return false;
        }

//限制图片的大小
        if (uploadImg.getSize() > 0.512 * 1024 * 1024) {
            imgInfo.setResult("error");
            imgInfo.setMessage("请上传512kb以内的文件!");
            return false;
        }
        return true;
    }

猜你喜欢

转载自blog.csdn.net/qq_29048719/article/details/77006879