package bj.ca.test.test00; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.util.Base64; /** * base64转换图片 */ public class Base642Jpg { public static void main(String[] args) { // String base64Str = ""; // 这里应该是您的Base64编码字符串 String imagePath = "D:\\李.jpg"; // 图片将被保存到这个路径 // 解码Base64字符串 byte[] imageBytes = Base64.getDecoder().decode(getImgBase64Str("D:\\testphoto1.txt")); // byte[] imageBytes = Base64.getDecoder().decode(base64Str); try { // 将字节数组转换为图片 ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes); BufferedImage image = ImageIO.read(bis); bis.close(); // 将图片写入文件系统 File outputFile = new File(imagePath); ImageIO.write(image, "jpg", outputFile); } catch (IOException e) { e.printStackTrace(); } } public static byte[] getImgBase64Str(String imgFile) { // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 InputStream in = null; byte[] data = null; // 读取图片字节数组 try { in = new FileInputStream(imgFile); data = new byte[in.available()]; int count = 0; while (in.read(data) > 0) { } } catch (IOException e) { } finally { try { if (in != null) { in.close(); } } catch (IOException e) { } } return data; } }
Base64转图片
最新推荐文章于 2024-11-11 21:19:25 发布