【问题标题】:How and Why are PDF and Excel files Base 64 encoded?如何以及为什么对 PDF 和 Excel 文件进行 Base 64 编码?
【发布时间】:2013-07-28 10:45:30
【问题描述】:

我读到的Base64编码的应用是将二进制数据或一些字符串转换为Base64格式。 但我知道文件(例如:PDF、Excel)本身是 Base64 编码的,它们甚至无法被相应的软件打开/支持。

我的问题是:

  1. 我们能否将整个文件编码为 Base64。
  2. 这个的应用场景是什么。
  3. 我们可以通过查看内容来知道要使用哪个解码器。

(仅供参考:我已阅读 wiki Base64)

【问题讨论】:

  • 1) 很容易回答。当然,我们可以使用 base64 对整个文件进行编码。 2)-ish。当您想要通过 7 位介质传输 8 位二进制文​​件时(例如,在 ASCII 模式下通过 FTP 传输非文本文件时,或在每封邮件发送二进制文件时。(邮件仅支持文本,FTP 默认为 7位来节省 1/8 的带宽。这在 300 位调制解调器的时代非常有用)。
  • PDF 不是 Base64 编码的(编码后它将不再是有效的 PDF),它支持 Base-64 编码用于存储它的一些对象。

标签: java base64 apache-commons-codec


【解决方案1】:

Java 中的 Base64 解码 我认为编码也可以用这个包中的一些类来完成

包装是

import org.apache.commons.codec.binary.Base64;

逻辑是

 String filepath = "C:\\Users\\sandeep\\somefile";
    String encodedString = null;

    try {
        File file=new File(filepath);
        FileReader fr = new FileReader(file);
        BufferedReader reader = new BufferedReader(fr);

        while ((encodedString = reader.readLine()) != null) {

            byte[] binOfEncoded = Base64.decodeBase64(encodedString.getBytes());  
            System.out.println("Base64 Decoded  String     :       " + binOfEncoded);               
        }

    } catch (IOException x) {
        System.err.format("IOException: %s%n", x);
    }

【讨论】:

    猜你喜欢
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    相关资源
    最近更新 更多