将本地图片转换Base64传输

该代码示例展示了如何在Java中利用Hutool库将本地路径的PNG图片读取为BufferedImage,然后将其转换为JPEG格式的BASE64字符串。主要涉及的类包括ImgUtil、ImageIO和File。
摘要由CSDN通过智能技术生成
将本地图片转换BASE64进行传输( 注意:本地图片路径最好是英文,否则可能报错)
import cn.hutool.core.img.ImgUtil;
import com.czxy.tanhua.AppServiceApplication;
import com.czxy.tanhua.autoconfig.template.AipTemplate;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @Description
 * @create 2023-03-31 9:55
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AppServiceApplication.class)
public class Aiptest {
    @Test
    public void Test() throws IOException {
        //本地图片
        String path = "D:\\jihe\\tupian\\sdf.png";
        //转换流
        BufferedImage read = ImageIO.read(new File(path));
        //转换base64
        String jpeg = ImgUtil.toBase64(read, "jpeg");
        System.out.println(jpeg);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值