import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import sun.misc.BASE64Decoder; public class testCode { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { FileInputStream fs = new FileInputStream("c://1.jpg"); byte[] b = new byte[fs.available()]; fs.read(b); fs.close(); String str=new String(b); //System.out.println("############"+str); String temp=getBASE64(str); System.out.println("编码为"+temp); System.out.println("解码为"+getFromBASE64(temp)); } // 将 s 进行 BASE64 编码 public static String getBASE64(String s) { if (s == null) return null; return (new sun.misc.BASE64Encoder()).encode( s.getBytes() ); } // 将 BASE64 编码的字符串 s 进行解码 public static String getFromBASE64(String s) { if (s == null) return null; BASE64Decoder decoder = new BASE64Decoder(); try { byte[] b = decoder.decodeBuffer(s); return new String(b); } catch (Exception e) { return null; } } } 本文转自:http://angelshand.blog.163.com/blog/static/83118637200911035159506/