【发布时间】:2018-08-29 04:10:24
【问题描述】:
好的,我有一些pdf需要通过base64encoder转换为base64。
最后,我使用解码器转换回 pdf 格式,但我的内容丢失了。
我的代码:
byte[] input_file = Files.readAllBytes(Paths.get("C:\\user\\Desktop\\dir1\\dir2\\test3.pdf"));
byte[] encodedBytes = Base64.getEncoder().encode(input_file);
String pdfInBase64 = new String(encodedBytes);
String originalString = new String(Base64.getDecoder().decode(encodedBytes));
System.out.println("originalString : " + originalString);
FileOutputStream fos = new FileOutputStream("C:\\user\\Desktop\\dir1\\dir2\\newtest3.pdf");
fos.write(originalString.getBytes());
fos.flush();
fos.close();
结果:
编码:https://pastebin.com/fnMACZzH
谢谢
【问题讨论】:
-
如果您只是将encodedBytes 写入文件,而不是通过String 泵送它然后写入String 的字节,会发生什么情况?
-
你不应该通过
originalString。保留原始解码字节。 -
@LouisWasserman 哦,我明白了,谢谢 :)
标签: java pdf base64 decode encode