文件上传MultipartFile实现图片压缩

MultipartFile上传图片并压缩

背景

之前写过一个图片上传,使用了spring的MultipartFile实现的,比较简单,限制了1M的大小,后来产品需求上传图片需压缩,遂着手开始实现

压缩工具

之前用过google的图片压缩工具thumbnailator,简单好用,决定这次也使用这个,具体使用可自行百度
github地址(https://github.com/coobird/thumbnailator), pom引用

		
        
            net.coobird
            thumbnailator
            0.4.8
        

代码

talk is cheap,show me the code,话不多说,直接贴代码

	@PostMapping("/uploadrespic")
    @ApiOperation(value = "上传文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "platform",  paramType = "query", dataType = "string", required = true),
            @ApiImplicitParam(name = "module",  paramType = "query", dataType = "string", required = true),
            @ApiImplicitParam(name = "type", paramType = "query", dataType = "string", required = true)
    })
    public ResponseEnvelope imgUpload(
            @RequestPart("file") MultipartFile file,
            @RequestParam String platform,
            @RequestParam String module,
            @RequestParam String type) {
        LoginUser loginUser = LoginContext.getLoginUser(LoginUser.class);
        if (loginUser == null) {
            return new ResponseEnvelope(false, "请先登录");
        }
        //上传图片压缩start
        //记录原MultipartFile,如果压缩异常就用原来的MultipartFile
        MultipartFile oldMultipartFile = file;
        try {
            String fileName = file.getName();
            String originalFilename = file.getOriginalFilename();
            String contentType = file.getContentType();

            LocalDateTime now = LocalDateTime.now();
            //这里就是生成一个绝对路径,使用了公司的工具类FilePathUtil,几个参数都没什么重要含义,里面引用太多,就不贴出来了,大家测试随便给个路径出来能构建file对象就好了
            String absolutePath = FilePathUtil.absolutePath(storagePath, platform, "temp", type, now, file.getOriginalFilename());
            File tempFile = new File(absolutePath);

            Thumbnails.of(file.getInputStream())
                    .scale(1f)
                    .outputQuality(0.99f)
                    .toFile(tempFile);

            FileInputStream fileInputStream = new FileInputStream(tempFile);
            file = new MockMultipartFile(fileName, originalFilename, contentType, fileInputStream);

            fileInputStream.close();
            boolean success = tempFile.delete();
            log.info(CLASS_LOG_PREFIX + "删除临时file success:{}", success);
        } catch (IOException e) {
            log.error(CLASS_LOG_PREFIX + "压缩图片失败,把MultipartFile赋值为原来的值oldFile,exception:{}", e);
            file = oldMultipartFile;
        }
		//上传图片压缩end
		
        LocalDateTime now = LocalDateTime.now();
        String absolutePath = FilePathUtil.absolutePath(storagePath, platform, module, type, now, file.getOriginalFilename());
        Uploader uploader = UploaderFactory.createUploader(type, file, absolutePath);

        if (file.getSize() > DEFAULT_LIMIT) {
            return new ResponseEnvelope(false, "请上传小于1M的图片");
        }
        String baseName = getBaseName(absolutePath) + FilePathUtil.getExtension(absolutePath).toUpperCase();
        Pattern compile = Pattern.compile(DEFAULT_RESFORMAT);
        Matcher matcher = compile.matcher(baseName);
        if (!matcher.find()) {
            return new ResponseEnvelope(false, "允许上传的图片格式为BMP\\JPG\\JPEG\\GIF\\PNG(格式不区分大小写)");
        }

        uploader.upload();
        FileVO fileVO = uploader.getFileVO();
        String returnPath = "/" + FilePathUtil.relativePath(storagePath, absolutePath);
        fileVO.setUrl(returnPath);
        //保存到数据库 并设置返回id
        Long picId = supplyDemandPicService.saveResPic(fileVO, type, absolutePath, loginUser, storagePath);
        fileVO.setResId(picId);
        if (picId == null || picId == 0) {
            return new ResponseEnvelope(false, "Failed to upload picture");
        }
        return new ResponseEnvelope(true, "Success to upload picture", fileVO);
    }

逻辑就在上传图片start和end中间的部分,没什么说的,就是把MultipartFile的输入流压缩到一个临时的file里,然后再根据临时file和之前的参数生成一个新的MultipartFile,完美的在之前的逻辑中嵌入了图片压缩,亲测有效,记录一下

后记

路还很长,不懂得还很多,坚持每天进步!!!

你可能感兴趣的:(java)