2013-07-28 38 views
0

我读过的Base64编码的应用是将二进制数据或一些字符串转换为Base64格式。 但我知道文件(例如:PDF,Excel)自己的Base64编码,甚至不能被相应的软件打开/支持。如何以及为什么PDF和Excel文件Base 64编码?

我的问题是:

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

(FYI:我已阅读维基Base64编码)在Java中

+0

1)很容易回答。当然,我们可以用base64编码整个文件。 2)-ish。当您想要在7位介质上传输8位二进制文​​件时(例如,在ASCII模式下通过FTP传输非文本文件时,或者每个邮件发送二进制文件时需要这样的操作(仅邮件支持文本,FTP默认为7位,以节省1/8的带宽,这对于300位调制解调器是非常有用的) – Hennes

+0

PDF *是*不是Base64编码(在编码后它不再是有效的PDF),它*支持* Base -64编码来存储其一些对象。 – usr2564301

回答

0

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); 
    } 
相关问题