In the project, you need to convert the BASE64 encoded picture into a picture file displayed in a Web project, Google search, you write a interchangeable tool class
Package com.base.util;
Import Java.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; /** * Picture and BASE64-encoded intermodulation tool class * @author Wangwei * * */public class Imagebase64util {public static void main (string[) args) {//test conversion from BASE64 encoding to picture file//String strimg = "xxxxx"; \ picture BASE64 string//Generateimage (strimg, "D
: \\wangyc.jpg ");
The test converts from picture file to Base64 encoded//SYSTEM.OUT.PRINTLN (GETIMAGESTR ("d:\\0.jpg"));
Generateimage (Getimagestr ("d:\\0.jpg"), "d:\\000.jpg"); /** * Converts a picture file into a byte array string and Base64 it for encoding * @param imgfilepath picture path * @return String/Pub
Lic static string Getimagestr (String imgfilepath) {byte[] data = null;
Reads the picture byte array try {InputStream in = new FileInputStream (Imgfilepath); data = new Byte[in.available ()];
In.read (data);
In.close ();
catch (IOException e) {e.printstacktrace ();
///To byte array Base64 encoding Base64encoder encoder = new Base64encoder (); return Encoder.encode (data)//Returns BASE64 encoded byte array string}/** * Base64 decoding of byte array strings and generating pictures * @param imgstr Base6 4 String * @param imgfilepath Generate picture Save path * @return Boolean/public static Boolean Generateimage (String img
STR, String Imgfilepath) {if (imgstr = = null)//image data is null return false;
Base64decoder decoder = new Base64decoder ();
try {//Base64 decode byte[] bytes = Decoder.decodebuffer (IMGSTR); for (int i = 0; i < bytes.length ++i) {if (bytes[i) < 0) {//Adjust exception data Bytes[i] +
= 256;
}///generate JPEG picture outputstream out = new FileOutputStream (Imgfilepath);
Out.write (bytes); Out.flush ();
Out.close ();
return true;
catch (Exception e) {return false; /** * Base64 decoding the byte array string and generating a picture * @param imgstr picture String * @return byte[] * * Public St
Atic byte[] Getstrtobytes (String imgstr) {if (IMGSTR = null)//image data is null return null;
Base64decoder decoder = new Base64decoder ();
try {//Base64 decode byte[] bytes = Decoder.decodebuffer (IMGSTR); for (int i = 0; i < bytes.length ++i) {if (bytes[i) < 0) {//Adjust exception data Bytes[i] +
= 256;
}///Generate JPEG picture return bytes;
catch (Exception e) {return null; }
}
}