java 图片base64互转
public class ImgBase64 { public static void main(String[] args) //测试 { String strImg = GetImageStr(); System.out.println(strImg); } public static String GetImageStr()//图片转base64 {//图片路径 String imgFile = "D:\\Users\\sys\\Pictures\\1.png";// InputStream in = null; byte[] data = null; try { in = new FileInputStream(imgFile); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data);// } public static boolean GenerateImage(String imgStr)//base字符串转图片 {// if (imgStr == null) // return false; BASE64Decoder decoder = new BASE64Decoder(); try { byte[] b = decoder.decodeBuffer(imgStr); for(int i=0;i<b.length;++i) { if(b[i]<0) //纠正错误字符 { b[i]+=256; } } String imgFilePath = "d:\\222.jpg";// OutputStream out = new FileOutputStream(imgFilePath); out.write(b); out.flush(); out.close(); return true; } catch (Exception e) { return false; } } }