0

我尝试将 Base64 字符串插入到 EnvelopedXmlObject 中的对象中:

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    dbFactory.setNamespaceAware(true);        
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document document = dBuilder.newDocument();  
    String generatedDocument = ...;

    BASE64Encoder encoder = new BASE64Encoder();
    String generatedDocumentEncoded = encoder.encode(generatedDocument .getBytes("UTF-8"));
    Node node1 = document.createTextNode(generatedDocumentEncoded);
    DataObjectDesc dataObjectDesc = new EnvelopedXmlObject   (node1, "plain/text", "http://www.w3.org/2000/09/xmldsig#base64");
    ....

当我在 file.xml 中运行此代码时,Object 的每一行都以“ ”结尾 导致解码文本错误

当我添加以下行时: Node node1 = document.createTextNode(generatedDocumentEncoded);

saveFile("c:\\temp\\temp.xml", generatedDocumentEncoded);
String stringFromFile = read("c:\\temp\\temp.xml");
node1.setTextContent(stringFromFile); 

然后生成的文件就可以了-我的错误在哪里?:)

4

1 回答 1

0

我认为您使用的 Base64 编码器会产生分块输出(将回车符放入字符串中)。您可以在输出字符串中替换此字符,如下所示:

generatedDocumentEncoded = generatedDocumentEncoded.replaceAll("(\r\n|\n)", "");
Node node1 = document.createTextNode(generatedDocumentEncoded);

或强制您的 BASE64Encoder 生成未分块的输出。我正在使用来自 Apache Commons的Base64类:

byte[] bytes = generatedDocument.getBytes("UTF-8");
String generatedDocumentEncoded = new String(Base64.encodeBase64(bytes, false), "UTF8");
于 2014-11-19T19:35:30.643 回答