Import Java.awt.image.BufferedImage; Import Java.io.ByteArrayInputStream; Import Java.io.ByteArrayOutputStream; Import Java.io.File; Import java.io.IOException; Import Javax.imageio.ImageIO; Import Sun.misc.BASE64Decoder; Import Sun.misc.BASE64Encoder; public class Testimagebinary {static Base64encoder encoder = new Sun.misc.BASE64Encoder (); Static Base64decoder decoder = new Sun.misc.BASE64Decoder (); public static void Main (string[] args) {System.out.println (Getimagebinary ()); Base64stringtoimage (Getimagebinary ()); } static String Getimagebinary () {file F = new File ("c://20090709442.jpg"); BufferedImage bi; try {bi = Imageio.read (f); Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); Imageio.write (BI, "JPG", BAOs); byte[] bytes = Baos.tobytearray (); Return Encoder.encodebuffer (bytes). Trim (); } catch (IOException e) {e.printstacktrace (); } return null; } static void Base64stringtoimage (String base64string) {try {byte[] Byte S1 = Decoder.decodebuffer (base64string); Bytearrayinputstream Bais = new Bytearrayinputstream (bytes1); BufferedImage bi1 =imageio.read (Bais); File W2 = new file ("C://qq.bmp");//Can be jpg,png,gif format imageio.write (bi1, "JPG", w2);//regardless of the output format picture, here do not need to change } catch (IOException e) {e.printstacktrace (); } } }
Encrypt and decrypt pictures using base64