在java中将base64解码的字节数组转换为String的问题

时间:2016-11-04 05:29:31

标签: java base64

在java中将base64解码的字节数组转换为String的问题:

 public static  String decode(String strcontent) throws Exception
 {
  BASE64Decoder decoder = new BASE64Decoder();
  byte[] imgBytes = decoder.decodeBuffer(strcontent);
  return new String(imgBytes);
 } 

使用上面的代码;我试图用Base 64解码字节数组(imgBytes)创建一个字符串。 input strcontent是base 64编码的字符串。对于文本文件,它工作正常,但对于PDF和图像文件,字符串转换存在问题。尝试过不同的编码,如UTF-8,UTF 16等。但没有用。返回的字符串与原始字符串不同。

尝试将字节数组写入如下文件时:

OutputStream out = new FileOutputStream(@@path);
out.write(imgBytes);
out.close();

正确创建文件而没有任何问题。

我尝试了以下代码:

byte[] imgBytes= ( new String(imgBytes1)).getBytes();  //Converting to    String and back to bytes
OutputStream out = new FileOutputStream(@@Filename); 
out.write(imgBytes); out.close(); 

这次图像文件已损坏。

请建议。

0 个答案:

没有答案