工具类-图片工具类ImageUtils

工具类-图片工具类ImageUtils.

功能:图片和base64互转,url和base64互转

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Objects;

/**
 * base64工具类
 */
@Slf4j
public class ImageUtils {
     
    private static final int BUF_SIZE = 8096;

    /**
     * 验证base64是否为图片
     *
     * @param base64Str
     * @return
     */
    public static boolean isImageBase64(String base64Str) {
     
        try {
     
            BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(Base64Utils.decodeFromString(base64Str)));
            if (Objects.equals(bufferedImage, (Object) null)) {
     
                return false;
            }
        } catch (IOException var2) {
     
            var2.printStackTrace();
        }

        return true;
    }

    /**
     * 根据url获取字节数组
     *
     * @param imgUrl
     * @return
     * @throws Exception
     */
    public static byte[] getBytesByUrl(String imgUrl) throws Exception {
     

        if (StringUtils.isEmpty(imgUrl)) {
     
            return null;
        }
        BufferedInputStream bis = null;
        ByteArrayOutputStream bos = null;
        HttpURLConnection http;
        URL url;

        try {
     
            url = new URL(imgUrl);
            http = (HttpURLConnection) url.openConnection();
            http.setConnectTimeout(3000);
            http.connect();
            bis = new BufferedInputStream(http.getInputStream());
            bos = new ByteArrayOutputStream();
            byte[] buf = new byte[BUF_SIZE];
            int size;
            while ((size = bis.read(buf)) != -1) {
     
                bos.write(buf, 0, size);
            }
            http.disconnect();
            return bos.toByteArray();
        } finally {
     
            if (bis != null) {
     
                try {
     
                    bis.close();
                } catch (IOException e) {
     
                    log.error("流关闭失败,原因:" + e.getMessage(), e);
                }
            }
            if (bos != null) {
     
                try {
     
                    bos.close();
                } catch (IOException e) {
     
                    log.error("流关闭失败,原因:" + e.getMessage(), e);
                }
            }
        }
    }

    /**
     * 根据url获取base64
     *
     * @param imgUrl
     * @return
     * @throws Exception
     */
    public static String getBase64ByUrl(String imgUrl) throws Exception {
     
        if (StringUtils.isEmpty(imgUrl)) {
     
            return null;
        }
        byte[] bytes = getBytesByUrl(imgUrl);
        if (bytes == null) {
     
            return "";
        }
        return Base64Utils.encodeToString(bytes);
    }

    /**
     * 将字节数组写为磁盘文件
     *
     * @param path
     * @param bytes
     */
    private void writeBytesToFile(String path, byte[] bytes) {
     
        if (bytes != null) {
     
            try {
     
                Files.write(Paths.get(path), bytes, StandardOpenOption.CREATE);
            } catch (Exception e) {
     
                log.error("写入文件出错了", e);
            }
        }
    }

    /**
     * 将base64写为磁盘文件
     *
     * @param path
     * @param base64
     */
    public void writeBase64ToFile(String path, String base64) {
     
        byte[] bytes = Base64Utils.decodeFromString(base64);
        writeBytesToFile(path, bytes);
    }
}

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