Java:Base64字符串转图片《或者》图片转Base64字符串

Java:Base64字符串转图片《或者》图片转Base64字符串

  1. 创建一个工具类,或可以创建一个类下将两个静态方法复制,使用本地绝对路劲进行测试
	//导入工程的对象
	static BASE64Encoder encoder = new sun.misc.BASE64Encoder();
	static BASE64Decoder decoder = new sun.misc.BASE64Decoder();

	/**
	 * 	读取图片:将图片转成base64二进制字符串
	 * @param filePath 图片路劲
	 * @param fileName 图片名称
	 * @return
	 */
	public static String imageToBase64String(String filePath,String fileName) {
		File f = new File(filePath+File.separator+fileName+".jpg");
		try {
			BufferedImage bi = ImageIO.read(f);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ImageIO.write(bi, "jpg", baos);
			byte[] bytes = baos.toByteArray();

			return encoder.encodeBuffer(bytes).trim();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 	生成图片:将base64二进制字符串生成图片,保存到某路劲下
	 * @param base64String  base64二进制字符串
	 * @param filePath  将存储文件路劲
	 * @param fileName  文件名称
	 */
	public static void base64StringToImage(String base64String,String filePath,String fileName) {
		try {
			byte[] bytes1 = decoder.decodeBuffer(base64String);
			ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
			BufferedImage bi1 = ImageIO.read(bais);
			File file = new File(filePath+File.separator+fileName+".jpg");
			File fileDir = new File(filePath+File.separator);
			if(!fileDir.exists()) {
				boolean mkdirs = fileDir.mkdirs();
			}
			ImageIO.write(bi1, "jpg", file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

测试方案事例:

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

public class TestImgTwoWayStr {

    static BASE64Encoder encoder = new sun.misc.BASE64Encoder();
    static BASE64Decoder decoder = new sun.misc.BASE64Decoder();


    public static void main(String[] args) {
        
        //本地测试,或者服务器测试,公用参数
        String filePath = "D:\\Image\\image2";  //这个是服务器地址或者本地绝对路劲哈
        String fileName = "timg";


        //事例1:将《图片Image》转成《Base64-Str》
        String base64String = imageToBase64String(filePath, fileName); //TODO
        

        //事例2:将《Base64-Str》转成《图片Image》
        base64StringToImage(base64String,filePath, fileName);  //TODO
        
    }


    public static String imageToBase64String(String filePath,String fileName) {
        File f = new File(filePath+File.separator+fileName+".jpg");
        try {
            BufferedImage bi = ImageIO.read(f);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bi, "jpg", baos);
            byte[] bytes = baos.toByteArray();

            return encoder.encodeBuffer(bytes).trim();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 	生成图片:将base64二进制字符串生成图片,保存到某路劲下
     * @param base64String  base64二进制字符串
     * @param filePath  将存储文件路劲
     * @param fileName  文件名称
     */
    public static void base64StringToImage(String base64String,String filePath,String fileName) {
        try {
            byte[] bytes1 = decoder.decodeBuffer(base64String);
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
            BufferedImage bi1 = ImageIO.read(bais);
            File file = new File(filePath+File.separator+fileName+".jpg");
            File fileDir = new File(filePath+File.separator);
            if(!fileDir.exists()) {
                boolean mkdirs = fileDir.mkdirs();
            }
            ImageIO.write(bi1, "jpg", file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值