package com.test; import org.junit.Test; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.*; /** *BASE64 */ public class Base64Test { @Test public void baseTest(){ //加密 String basestr = imageChangeBase64("D://1.png"); System.out.println(basestr); //解密 base64ChangeImage(basestr,"D://2.png"); } /** * 图片转BASE64 * @param imagePath 路径 * @return */ public String imageChangeBase64(String imagePath){ InputStream inputStream = null; byte[] data = null; try { inputStream = new FileInputStream(imagePath); data = new byte[inputStream.available()]; inputStream.read(data); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } // 加密 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data); } /** * BASE转图片 * @param baseStr base64字符串 * @param imagePath 生成的图片 * @return */ public boolean base64ChangeImage(String baseStr,String imagePath){ if (baseStr == null){ return false; } BASE64Decoder decoder = new BASE64Decoder(); try { // 解密 byte[] b = decoder.decodeBuffer(baseStr); // 处理数据 for (int i = 0; i < b.length; ++i) { if (b[i] < 0) { b[i] += 256; } } OutputStream out = new FileOutputStream(imagePath); out.write(b); out.flush(); out.close(); return true; } catch (Exception e) { return false; } } }
在线的base64图片转换带有:"data:image/jpeg;base64," 解码之前这个得去掉。
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/18558.html