1. import

    java.awt.image.BufferedImage;

  2. import

    java.io.ByteArrayInputStream;

  3. import

    java.io.ByteArrayOutputStream;

  4. import

    java.io.File;

  5. import

    java.io.IOException;

  6. import

    javax.imageio.ImageIO;

  7. import

    sun.misc.BASE64Decoder;

  8. import

    sun.misc.BASE64Encoder;

  9. public


    class

    TestImageBinary {

  10. static

    BASE64Encoder encoder =

    new

    sun.misc.BASE64Encoder();

  11. static

    BASE64Decoder decoder =

    new

    sun.misc.BASE64Decoder();

  12. public


    static


    void

    main(String[] args) {
  13. System.out.println(getImageBinary());
  14. base64StringToImage(getImageBinary());
  15. }

  16. static

    String getImageBinary(){
  17. File f =

    new

    File(

    “c://20090709442.jpg”

    );
  18. BufferedImage bi;

  19. try

    {
  20. bi = ImageIO.read(f);
  21. ByteArrayOutputStream baos =

    new

    ByteArrayOutputStream();
  22. ImageIO.write(bi,

    “jpg”

    , baos);

  23. byte

    [] bytes = baos.toByteArray();

  24. return

    encoder.encodeBuffer(bytes).trim();
  25. }

    catch

    (IOException e) {
  26. e.printStackTrace();
  27. }

  28. return


    null

    ;
  29. }

  30. static


    void

    base64StringToImage(String base64String){

  31. try

    {

  32. byte

    [] bytes1 = decoder.decodeBuffer(base64String);
  33. ByteArrayInputStream bais =

    new

    ByteArrayInputStream(bytes1);
  34. BufferedImage bi1 =ImageIO.read(bais);
  35. File w2 =

    new

    File(

    “c://QQ.bmp”

    );//可以是jpg,png,gif格式
  36. ImageIO.write(bi1,

    “jpg”

    , w2);

    //不管输出什么格式图片,此处不需改动
  37. }

    catch

    (IOException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. }