问题
I tried to insert Base64 string into Object in 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");
....
When i run this code in file.xml each line of Object is ended with " " which cause an error with decoding text
When I add this lines: Node node1 = document.createTextNode(generatedDocumentEncoded);
saveFile("c:\\temp\\temp.xml", generatedDocumentEncoded);
String stringFromFile = read("c:\\temp\\temp.xml");
node1.setTextContent(stringFromFile);
then generated file is ok - where is my mistake ? :)
回答1:
I think that Base64 encoder You use will produce chunked output (put carriage returns in string). You can replace in output string this characters like below:
generatedDocumentEncoded = generatedDocumentEncoded.replaceAll("(\r\n|\n)", "");
Node node1 = document.createTextNode(generatedDocumentEncoded);
or force Your BASE64Encoder generate not chunked output. I'm using for that Base64 class from Apache Commons:
byte[] bytes = generatedDocument.getBytes("UTF-8");
String generatedDocumentEncoded = new String(Base64.encodeBase64(bytes, false), "UTF8");
来源:https://stackoverflow.com/questions/26293066/xades4j-and-base64-in-envelopedxmlobject