【问题标题】:Error decoding base64 string解码 base64 字符串时出错
【发布时间】:2013-02-12 09:34:02
【问题描述】:

我有一个 XML 文件,它有一个名为“CONTENIDO”的节点,在这个节点中我有一个用 base64 字符串编码的 PDF 文件。

我正在尝试读取此节点,解码 base64 中的字符串并将 PDF 文件下载到我的计算机。

问题是文件以与原始 PDF 相同的大小(以 kb 为单位)下载,并且具有相同的页数,但是......所有页面都是空白的,没有任何内容,当我打开下载的文件会出现一个弹出窗口,并显示错误消息“unknown distinct 806.6n”。我不知道那是什么意思。

我试图在互联网上找到一个解决方案,用不同的方法来解码字符串,但总是得到相同的结果...... XML 没问题我检查了 base64 字符串,没问题。 我也调试了代码,我看到我正在读取base64字符串的var“fichero”的内容也可以,所以我不知道可能是什么问题。

这是我的代码:

package prueba.sap.com;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import sun.misc.BASE64Decoder;

import javax.xml.bind.DatatypeConverter;

public class anexoPO {


    public static void main(String[] args) throws Exception {
         FileInputStream inFile =
              new FileInputStream("C:/prueba/prueba_attach_b64.xml");
         FileOutputStream outFile =
              new FileOutputStream("C:/prueba/salida.pdf");      
         anexoPO myMapping = new anexoPO();
         myMapping.execute(inFile, outFile);
         System.out.println("Success");
         System.out.println(inFile);         

    }

    public void execute(InputStream in, OutputStream out)
     throws com.sap.aii.mapping.api.StreamTransformationException {

        try {       

          //************************Code To Generate The XML Parsing Objects*****************************//     
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder db = dbf.newDocumentBuilder();
          Document doc = db.parse(in);
          Document docout = db.newDocument();

          NodeList CONTENIDO = doc.getElementsByTagName("CONTENIDO");
          String fichero = CONTENIDO.item(0).getChildNodes().item(0).getNodeValue();

          //************** decode *************/

              //import sun.misc.BASE64Decoder;
              //BASE64Decoder decoder = new BASE64Decoder();
              //byte[] decoded = decoder.decodeBuffer(fichero);

              //import org.apache.commons.codec.binary.*;
              //byte[] decoded = Base64.decode(fichero);

              //import javax.xml.bind.DatatypeConverter;
               byte[] decoded = DatatypeConverter.parseBase64Binary(fichero);

          //************** decode *************/

          String str = new String(decoded);
          out.write(str.getBytes());

          } catch (Exception e) {
               System.out.print("Problem parsing the file");
               e.printStackTrace();
              }       
    }

}

提前致谢。

【问题讨论】:

    标签: java base64


    【解决方案1】:

    肯定的:

    out.write(decoded);
    out.close();
    

    字符串不能代表所有字节,PDF是二进制的。

    同时删除 sun.misc.BASE64Decoder 的导入,因为这个包并不存在于任何地方。它可能会被编译器删除,但我不会打赌。

    【讨论】:

      猜你喜欢
      • 2019-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-18
      • 1970-01-01
      相关资源
      最近更新 更多