SpringBoot压缩图片及获取url图片大小

application.properties新增配置

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=20MB

增加依赖

  
        
            net.coobird
            thumbnailator
            0.4.8
        

        
        
            org.projectlombok
            lombok
            1.16.10
            provided
        

新增工具类BASE64DecodedMultipartFile

package com.example.demo;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;


public class BASE64DecodedMultipartFile implements MultipartFile {
    private final byte[] imgContent;
    private final String header;

    public BASE64DecodedMultipartFile(byte[] imgContent, String header) {
        this.imgContent = imgContent;
        this.header = header.split(";")[0];
    }

    @Override
    public String getName() {
        // TODO - implementation depends on your requirements
        return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
    }

    @Override
    public String getOriginalFilename() {
        // TODO - implementation depends on your requirements
        return System.currentTimeMillis() + (int)Math.random() * 10000 + "." + header.split("/")[1];
    }

    @Override
    public String getContentType() {
        // TODO - implementation depends on your requirements
        return header.split(":")[1];
    }

    @Override
    public boolean isEmpty() {
        return imgContent == null || imgContent.length == 0;
    }

    @Override
    public long getSize() {
        return imgContent.length;
    }

    @Override
    public byte[] getBytes() throws IOException {
        return imgContent;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(imgContent);
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        new FileOutputStream(dest).write(imgContent);
    }



}


新增工具类ImageSizeUtil

package com.example.demo;

import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 获取图片尺寸和截图 工具类
 *
 * 
 */
@Component
@Slf4j
public class ImageSizeUtil {


    /**
     * 获取图片最长边长度
     *
     * @param params
     * @return
     */
    public static int getImageLengthOfSide(MultipartFile params) {
        int lengthSize = 0;
        Map result = new HashMap<>();
        long beginTime = new Date().getTime();
        // 获取图片格式
        String suffixName = getSuffixNameInfo(params);
        try {
            Iterator readers = ImageIO.getImageReadersByFormatName(suffixName);
            ImageReader reader = (ImageReader) readers.next();
            ImageInputStream iis = ImageIO.createImageInputStream(params.getInputStream());
            reader.setInput(iis, true);
            result.put("width", reader.getWidth(0));
            result.put("height", reader.getHeight(0));
            if (reader.getWidth(0) > reader.getHeight(0)) {
                lengthSize = reader.getWidth(0);
            } else {
                lengthSize = reader.getHeight(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return lengthSize;
    }

    /**
     * 获取图片格式
     *
     * @param params
     * @return
     */
    public static String getSuffixNameInfo(MultipartFile params) {
        String result = "";
        // 图片后缀
        String suffixName = params.getOriginalFilename().substring(
                params.getOriginalFilename().lastIndexOf("."));
        if (suffixName.indexOf("png") > 0) {
            result = "png";
        } else if (suffixName.indexOf("jpg") > 0) {
            result = "jpg";
        } else if (suffixName.indexOf("jpeg") > 0) {
            result = "jpeg";
        }

        return result;
    }


    /**
     * 根据指定大小压缩图片
     *
     * @param imageBytes  源图片字节数组
     * @param desFileSize 指定图片大小,单位kb
     * @return 压缩质量后的图片字节数组
     */
    public static byte[] compressPicForScale(byte[] imageBytes, long desFileSize) {
        if (imageBytes == null || imageBytes.length <= 0 || imageBytes.length < desFileSize * 1024) {
            return imageBytes;
        }
        long srcSize = imageBytes.length;
        double accuracy = getAccuracy(srcSize / 1024);
//        double accuracy = 0.85;
        try {
            while (imageBytes.length > desFileSize * 1024) {
                ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream(imageBytes.length);
                Thumbnails.of(inputStream)
                        .scale(accuracy)
                        .outputQuality(accuracy)
                        .toOutputStream(outputStream);
                imageBytes = outputStream.toByteArray();
            }
            log.info("【图片压缩】imageId={} | 图片原大小={}kb | 压缩后大小={}kb",
                    "", srcSize / 1024, imageBytes.length / 1024);
        } catch (Exception e) {
            log.error("【图片压缩】msg=图片压缩失败!", e);
        }
        return imageBytes;
    }

    /**
     * 自动调节精度(经验数值)
     *
     * @param size 源图片大小,单位kb
     * @return 图片压缩质量比
     */
    private static double getAccuracy(long size) {
        double accuracy;
        if (size < 900) {
            accuracy = 0.85;
        } else if (size < 2047) {
//            accuracy = 0.6;
            accuracy = 0.8;
        } else if (size < 3275) {
//            accuracy = 0.44;
            accuracy = 0.7;
        } else {
            accuracy = 0.4;
        }
        return accuracy;
    }

    /**
     * base64 转MultipartFile
     *
     * @param base64
     * @return
     */
    public static MultipartFile base64ToMultipart(String base64) {
        try {
            // 注意base64是否有头信息,如:data:image/jpeg;base64,。。。
            String[] baseStrs = base64.split(",");

            BASE64Decoder decoder = new BASE64Decoder();
            byte[] b = new byte[0];
            b = decoder.decodeBuffer(baseStrs[1]);

            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }

            return new BASE64DecodedMultipartFile(b, baseStrs[0]);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 压缩图片
     *
     * @return
     */
    public static MultipartFile byte2Base64StringFun(MultipartFile fileImg) {
        MultipartFile result = fileImg;
        BASE64Encoder encoder = new BASE64Encoder();
        String imgData1 = null;
        try {
            InputStream inputStream = fileImg.getInputStream();
            byte[] imgData = ImageSizeUtil.compressPicForScale(ToolsUtil.getByteArray(inputStream), 1024);
            imgData1 = "data:" + fileImg.getContentType() + ";base64," + encoder.encode(imgData);
            MultipartFile def = ImageSizeUtil.base64ToMultipart(imgData1);
            result = def;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


}



可修改ImageSizeUtil 里面的byte2Base64StringFun方法,选择压缩到多少kb以下

新增工具类ToolsUtil

package com.example.demo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 公共 工具类
 * 
 */
public class ToolsUtil {

    /**
     * InputStream转换为byte[]
     *
     * @param inStream
     * @return
     * @throws IOException
     */
    public static final byte[] getByteArray(InputStream inStream) throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while ((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        return in2b;
    }


}


测试

package com.example.demo;



import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


@RestController
@RequestMapping("/upload")
public class FastDFSUpLoadController {



    @Resource
    private ImageSizeUtil imageSizeUtil;

    @PostMapping("/imgUpload")
    public void imgup(MultipartFile file) throws Exception {

        file=imageSizeUtil.byte2Base64StringFun(file);
        //省略上传fdfs

    }

}

在这里插入图片描述

获取url图片大小

 /**
     * 获取网络文件大小
     * @param url
     * @param type
     * @return
     * @throws IOException
     */
    private int getFileLength(String url1) throws IOException{
        int length=0;
        URL url;
        try {
            url = new  URL(url1);
            HttpURLConnection urlcon=(HttpURLConnection)url.openConnection();
            //根据响应获取文件大小
            length=urlcon.getContentLength();
            urlcon.disconnect();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return length;
    }

你可能感兴趣的:(spring,boot,java,spring)