Base64加密解密

jdk1.7以下

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class Base64ImgHelper {
	static Logger logger = LoggerFactory.getLogger(Base64ImgHelper.class);

	// base64字符串转化成图片
	public static boolean GenerateImage(String imgStr, String imgFilePath) { // 对字节数组字符串进行Base64解码并生成图片
		if (imgStr == null) // 图像数据为空
			return false;
		BASE64Decoder decoder = new BASE64Decoder();
		try (OutputStream out = new FileOutputStream(imgFilePath)) {
			// Base64解码
			byte[] b = decoder.decodeBuffer(imgStr);
			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {// 调整异常数据
					b[i] += 256;
				}
			}
			// 生成jpeg图片

			out.write(b);
			out.flush();
			out.close();
			return true;
		} catch (Exception e) {
			System.out.println("图片转换失败" + e.getMessage());
			logger.error("图片转换失败" + e.getMessage(), e);
			return false;
		}
	}

	// base64字符串转化成图片
	public static byte[] base64ToByte(String imgStr) { // 对字节数组字符串进行Base64解码并生成图片
		if (imgStr == null) // 图像数据为空
			return null;
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			// Base64解码
			byte[] b = decoder.decodeBuffer(imgStr);
			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {// 调整异常数据
					b[i] += 256;
				}
			}
			return b;
		} catch (Exception e) {
			System.out.println("图片转换失败" + e.getMessage());
			logger.error("图片转换失败" + e.getMessage(), e);
			return null;
		}
	}

	public static String byteToBase64(byte[] data) throws IOException {
		// 加密
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(data).replaceAll("\n", "");
	}

	/**
	 * @Description: 根据图片地址转换为base64编码字符串
	 * @Author:
	 * @CreateTime:
	 * @return
	 * @throws IOException
	 */
	public static String getImageStr(String imgFile) throws IOException {
		try (InputStream inputStream = new FileInputStream(imgFile)) {
			byte[] data = null;
			data = new byte[inputStream.available()];
			int count = inputStream.read(data);
			if (count <= 0) {
				throw new RuntimeException("图片" + imgFile + "不存在!");
			}
			inputStream.close();
			// 加密
			BASE64Encoder encoder = new BASE64Encoder();
			return encoder.encode(data).replaceAll("\n", "");
		} catch (Exception e) {
			throw new RuntimeException("图片" + imgFile + "不存在!");
		}

	}
}

jdk1.8+

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Base64ImgHelper {
	static Logger logger = LoggerFactory.getLogger(Base64ImgHelper.class);

	// base64字符串转化成图片
	public static boolean GenerateImage(String imgStr, String imgFilePath) { // 对字节数组字符串进行Base64解码并生成图片
		if (imgStr == null) // 图像数据为空
			return false;
		Decoder decoder = Base64.getDecoder();
		try (OutputStream out = new FileOutputStream(imgFilePath)) {
			// Base64解码
			byte[] b = decoder.decode(imgStr);
			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {// 调整异常数据
					b[i] += 256;
				}
			}
			// 生成jpeg图片

			out.write(b);
			out.flush();
			out.close();
			return true;
		} catch (Exception e) {
			System.out.println("图片转换失败" + e.getMessage());
			logger.error("图片转换失败" + e.getMessage(), e);
			return false;
		}
	}

	// base64字符串转化成图片
	public static byte[] base64ToByte(String imgStr) { // 对字节数组字符串进行Base64解码并生成图片
		if (imgStr == null) // 图像数据为空
			return null;
		Decoder decoder = Base64.getDecoder();
		try {
			// Base64解码
			byte[] b = decoder.decode(imgStr);
			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {// 调整异常数据
					b[i] += 256;
				}
			}
			return b;
		} catch (Exception e) {
			System.out.println("图片转换失败" + e.getMessage());
			logger.error("图片转换失败" + e.getMessage(), e);
			return null;
		}
	}

	public static String byteToBase64(byte[] data) throws IOException {
		// 加密
		Encoder encoder = Base64.getEncoder();
		String encode = encoder.encodeToString(data).replaceAll("\n", "");
		return encode;
	}

	/**
	 * @Description: 根据图片地址转换为base64编码字符串
	 * @Author:
	 * @CreateTime:
	 * @return
	 * @throws IOException
	 */
	public static String getImageStr(String imgFile) throws IOException {
		try (InputStream inputStream = new FileInputStream(imgFile)) {
			byte[] data = null;
			data = new byte[inputStream.available()];
			int count = inputStream.read(data);
			if (count <= 0) {
				throw new RuntimeException("图片" + imgFile + "不存在!");
			}
			inputStream.close();
			// 加密
			Encoder encoder = Base64.getEncoder();
			String encode = encoder.encodeToString(data).replaceAll("\n", "");
			return encode;
		} catch (Exception e) {
			throw new RuntimeException("图片" + imgFile + "不存在!");
		}

	}
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值