1 public static void main(String args[]) throws Exception { 2 InputStream in = null; 3 byte[] data = null; 4 in = new FileInputStream("C:\\Users\\HP\\Pictures\\unicom.jpg"); 5 data = new byte[in.available()]; 6 in.read(data); 7 in.close(); 8 BASE64Encoder encoder = new BASE64Encoder(); 9 // 對字節數組Base64第一次編碼 10 String base64Encode1 = encoder.encode(data); 11 // 對字節數組Base64再次編碼 12 String base64Encode2 = encoder.encode(base64Encode1.getBytes()); 13 System.out.println(base64Encode2); 14 15 BASE64Decoder decoder = new BASE64Decoder(); 16 //base64第一次解碼 17 byte[] base64Decode1 = decoder.decodeBuffer(base64Encode2); 18 //Base64再次解碼 19 byte[] base64Decode2 = decoder.decodeBuffer(new String(base64Decode1)); 20 // System.out.println("解碼完成"); 21 for(int i=0;i<base64Decode2.length;++i) 22 { 23 if(base64Decode2[i]<0) 24 {//調整異常數據 25 base64Decode2[i]+=256; 26 } 27 } 28 //生成jpg圖片 29 OutputStream out = new FileOutputStream("C:\\Users\\HP\\Pictures\\unicom2.jpg"); 30 out.write(base64Decode2); 31 out.flush(); 32 out.close(); 33 }