base64使用
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 }