工具类-图片工具类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);
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值