Base64编码与图片 互转

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Base64;

/**
 * 
 * @author shilei
 *
 * @time 2019年6月20日 下午4:53:05
 *
 * @desc 图片与Base64 转换
 */
public class ToolBase64 {

	/**
	 * @param base64编码字符串
	 * @param path-具体到文件
	 * @Desc: 将base64编码字符串转换为图片
	 */
	public static String decryptByBase64(String base64, String filePath) {
		if (base64 == null || filePath == null)
			return "生成文件失败,请给出相应的数据";

		try {
			Files.write(Paths.get(filePath), Base64.getDecoder().decode(base64), StandardOpenOption.CREATE);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "指定路径下生成文件成功!";
	}

	/**
	 * @Desc: 根据图片地址转换为base64编码字符串
	 */
	public static String encryptToBase64(String filePath) {
		if (filePath == null)
			return "生成文件失败,请给出相应的数据";
		try {
			byte[] b = Files.readAllBytes(Paths.get(filePath));
			return Base64.getEncoder().encodeToString(b);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
发布了65 篇原创文章 · 获赞 21 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_39028580/article/details/103202014