我从webservice调用获取JSON响应
] 1
我必须将此文件保存到我的系统中。我正在使用以下代码:
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(response);
ByteArrayInputStream bis = new ByteArrayInputStream(decodedBytes);
bis.close();
File file = new File("C:/image one.jpeg");
FileOutputStream fop = new FileOutputStream(file);
fop.write(decodedBytes);
fop.flush();
fop.close();
文件已保存但已损坏,无法打开。有人可以阐明我做错了什么吗?
最佳答案
尝试使用Base64
中的java.util.Base64
类
byte[] decodedBytes = Base64.getDecoder().decode(base64String);
或
Base64
中的org.apache.commons.codec.binary
byte[] decodedBytes = Base64.decodeBase64(base64String);
而且您不需要
ByteArrayInputStream
对象请参阅此link以获取更详细的答案