base64 encoded transfer pictures, pictures turn base64 encoding

Pro-test available:

Code:

. 1  Import sun.misc.BASE64Decoder;
 2  Import sun.misc.BASE64Encoder;
 . 3  
. 4  Import the java.io. * ;
 . 5  
. 6  public  class Base64Util {
 . 7  
. 8      / * to convert base64 encoded image * / 
. 9      public  static  Boolean generateImage (String imgStr, String path) {
 10          IF (imgStr == null )
 . 11              return  to false ;
 12 is          Base64Decoder Decoder = new new Base64Decoder ();
 13 is          the try {
14             byte[] b = decoder.decodeBuffer(imgStr);
15             for (int i = 0; i < b.length; i++) {
16                 if (b[i] < 0) {
17                     b[i] += 256;
18                 }
19             }
20             OutputStream out = new FileOutputStream(path);
21             out.write(b);
22             out.flush();
23             out.close();
24             return true;
25         } catch(Exception E) {
 26 is              e.printStackTrace ();
 27              return  to false ;
 28          }
 29      }
 30  
31 is      / * the picture is converted to a base64 encoded * / 
32      public  static String getImageStr (String imgfile) {
 33 is          the InputStream inputStream = null ;
 34 is          byte [] Data = null ;
 35          the try {
 36              inputStream = new new the FileInputStream (imgfile);
 37 [              Data = new new  byte[inputStream.available ()];
 38 is              InputStream.read (Data);
 39              inputStream.close ();
 40          } the catch (IOException E) {
 41 is              e.printStackTrace ();
 42 is          }
 43 is          Base64Encoder Encoder = new new Base64Encoder ();
 44 is          return encoder.encode (Data);
 45      }
 46 is      / * this is the use of the above method, can be ignored * / 
47      / * public static void main (String [] args) {
 48          * // * the picture converting base64 encoded * * // 
49         String strImg = getImageStr ( "Image transfer path where: c: / users / image .png");
 50          System.out.println (strImg);
 51 is          * // * to convert base64 encoded image * // * 
52 is          generateImage (strImg, strImg * // * strImg here is base64 encoded * @ * );
 53      } * / 
54 }

 

Guess you like

Origin www.cnblogs.com/wangquanyi/p/11328907.html