我收到的是 Base 64 编码的 PDF 内容。我尝试使用 NIFI 和 Processor Base64EncodeContent 对其进行解码。我通过邮件发送的解码文件。以下是邮件中输出的小样本。
“中不应有任何数据。 ¹ 检查是否已发送 . . 所有文档均以 pdf 形式发送至* 9 : ' ³: > < âA mØ'²@%é‚ÇŽÇœ|ÀÈ™$Éز§Uû ÷LÒTBù l,îåù~$â´º?6N-JC¤ŒÒ_Ïg -æ¿;ž‰ìÛÖYl`õ?èÓÌ[ ÿÿ PK"
如何提取第三方发送的PDF数据?
我尝试使用 JAVA 代码对其进行解码,但也失败了。无法打开 PDF,也出现垃圾字符。
下面使用的ConvertedJPGPDF.pdf文件包含Base64编码的字符串。
String filePath = "C:\\Users\\xyz\\Desktop\\";
String originalFileName = "ConvertedJPGPDF.pdf";
String newFileName = "test.pdf";
byte[] input_file =
Files.readAllBytes(Paths.get(filePath+originalFileName));
// byte[] decodedBytes = Base64.getDecoder().decode(input_file);
byte[] decodedBytes1 = Base64.getMimeDecoder().decode(input_file);
FileOutputStream fos = new FileOutputStream(filePath+newFileName);
fos.write(decodedBytes1);
fos.flush();
fos.close();
最佳答案
您提到该文件已包含 base64 编码的字符串。
ConvertedJPGPDF.pdf file used below contains Base64 encoded String.
因此,您不需要运行此行:
byte[] encodedBytes = Base64.getEncoder().encode(input_file);
通过这样做,您将尝试再次对这些字节进行编码。
直接解码input_file
数组,然后将得到的字节数组保存到.pdf文件中。
更新:
ConvertedJPGPDF.pdf
实际上不必命名为 .pdf
。考虑到它是 Base 64 编码的,它实际上是一个纯文本文件。
无论如何,以下代码对我有用:
String filePath = "C:\\Users\\xyz\\Desktop\\";
String originalFileName = "ConvertedJPGPDF.pdf";
String newFileName = "test.pdf";
byte[] input_file = Files.readAllBytes(Paths.get(filePath+originalFileName));
byte[] decodedBytes1 = Base64.getMimeDecoder().decode(input_file);
Files.write(Paths.get(filePath+newFileName), decodedBytes1);
<小时/>
希望这有帮助!
关于java - 解码 Base 64 数据时出现垃圾字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55354281/