java - 解码 Base 64 数据时出现垃圾字符

标签 java base64 apache-nifi

我收到的是 Base 64 编码的 PDF 内容。我尝试使用 NIFI 和 Processor Base64EncodeContent 对其进行解码。我通过邮件发送的解码文件。以下是邮件中输出的小样本。

enter image description here

“中不应有任何数据。 ¹ 检查是否已发送 . . 所有文档均以 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/

相关文章:

queue - NiFi如何清除所有队列

hadoop - Sqoop 与 SQL Anywhere 17 连接

java - jfreechart 缩小监听器

java - 在 spring-data 中为 MongoDB 配置 GridFS 模板

java - Spring Security - 多个安全配置要求 - 将基本身份验证添加到现有的 REST API 身份验证

java - 使用 base64 编码器和 InputStreamReader 的问题

angular - 如何以 Angular 调整base64图像的大小

ios - 如何在 Swift 中从 Base64 编码的字符串创建图像?

java - 如何阻止 Eclipse 在数组实例化及其值之间创建新行(Ctrl + Shift + F)?

apache-nifi - NiFi XML 到 JSON