我从 Web 服务调用中获取 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();
文件已保存,但已损坏且无法打开。有人可以阐明我做错了什么吗?
最佳答案
尝试使用java.util.Base64
中的Base64
类
byte[] decodedBytes = Base64.getDecoder().decode(base64String);
或者来自org.apache.commons.codec.binary
的Base64
byte[] decodedBytes = Base64.decodeBase64(base64String);
并且您不需要 ByteArrayInputStream
对象
查看此link更详细的答案
关于java - 将 Base64 字符串保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53508600/