java apache IOUtils 破坏文件内容

java apache IOUtils breaks file content

我需要encode/decode将pdf文件转换成Base64格式。 所以我将文件从磁盘读入字符串(因为我将来会收到字符串 Base64 格式的文件);

String pdfString = IOUtils.toString(new FileInputStream(new
            File("D:\vrpStamped.pdf")));
byte[] encoded = Base64.encodeBase64(pdfString.getBytes());

byte[] newPdfArray = Base64.decodeBase64(encoded);
FileOutputStream imageOutFile = new FileOutputStream(
        "D:\1.pdf");
imageOutFile.write(newPdfArray);
imageOutFile.close();
imageOutFile.flush();

所以我的 D:\1.pdf 没有在 Adob​​eReader 中打开,但是如果我直接读取文件到字节数组,而不是使用 IOUtils.toByteArray(..),一切正常,我的 D:\1.pdf文件在 Adob​​e 中成功打开 Reader:

byte[] encoded = Base64.encodeBase64(IOUtils.toByteArray(new FileInputStream(new File("D:\vrpStamped.pdf"))););

在我看来 IOUtils.toString(..) 更改了文件内容中的某些内容。那么如何将文件转换为不破坏内容的字符串?

如何编码 pdf...

byte[] bytes = IOUtils.toByteArray(new FileInputStream(new File("/home/fschaetz/test.pdf")));
byte[] encoded = Base64.encode(bytes);
String str = new String(encoded);

...现在用这个编码的字符串做一些事情,例如,通过 Rest 服务发送它。

现在,如果您收到经过编码的字符串,您可以像这样解码并保存它...

byte[] decoded = Base64.decode(str.getBytes());
FileOutputStream output = new FileOutputStream(new File("/home/fschaetz/result.pdf"));
output.write(decoded);
output.close();

适用于所有文件,不限于图像或 pdf。

您的示例所做的是...

  1. 将 pdf 读入字符串(这几乎会破坏数据,因为您正在将二进制数据读入字符串)
  2. 对此 spring 进行编码(这很可能不再是原始 pdf 的有效表示)
  3. 解码并保存到磁盘