I won't say much nonsense, I will just post the conversion code between Java implementation image and base84 string. The specific code is as follows:
package cn.com; import <a href="http://lib.csdn.net/base/javase" class='replace_word' target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class Base64Test { public static void main(String[] args) { String strImg = GetImageStr(); System.out.println(strImg); GenerateImage(strImg); } //Convert the image into a base64 string public static String GetImageStr() {//Convert the image file into a byte array string and base64 encoding it String imgFile = "d://test.jpg";//Please InputStream in = null; byte[] data = null; //Read the byte array of the image try { in = new FileInputStream(imgFile); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } //Encoding the byte array Base64 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data); //Return Base64 encoded byte array string} //Convert base64 string into image public static boolean GenerateImage(String imgStr) { //Base64 decodes the byte array string and generates an image if (imgStr == null) //The image data is empty return false; BASE64Decoder decoder = new BASE64Decoder(); try { //Base64 decode byte[] b = decoder.decodeBuffer(imgStr); for(int i=0;i<b.length;++i) { if(b[i]<0) {//Adjust exception data b[i]+=256; } } //Generate jpeg image String imgFilePath = "d://222.jpg";//Newly generated image OutputStream out = new FileOutputStream(imgFilePath); out.write(b); out.flush(); out.close(); return true; } catch (Exception e) { return false; } } }The above is the Java conversion between pictures and base64 strings introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!