have been doing this lately, so the code will be a little more
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;
public class Test64bit {public static void main (string[] args) {//test convert from BASE64 encoding to picture file String strimg = "Base64 encoding";
Generateimage (strimg, "c:\\ test. jpg");
The test converts from picture file to Base64 encoded SYSTEM.OUT.PRINTLN (GETIMAGESTR ("c:\\wangyc.jpg"));
The public static string Getimagestr (String imgfilepath) {//Converts a picture file into a byte array string and BASE64 encoding it byte[] data = null; Read the picture byte array try {InputStream in = new FileInputStream (imgfilepath); data = new byte[in.available ()]; in.read (data); in.cl
OSE ();
catch (IOException e) {e.printstacktrace ();}
BASE64 encoding the byte array Base64encoder encoder = new Base64encoder (); return Encoder.encode (data)//Returns BASE64 encoded byte array string} public static Boolean Generateimage (String imgstr, String Imgfilepa
TH) {///Base64 decoding of byte array strings and generating picture if (IMGSTR = = null)//image data is null return false; BasE64decoder decoder = new Base64decoder (); try {//Base64 decoding byte[] bytes = Decoder.decodebuffer (IMGSTR); for (int i = 0; i < bytes.length; ++i) {if (bytes[i) &L T
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;}} }
Transferred from blog.sina.com.cn/s/blog_81a2e91401010214.html
20140513