gpt4 book ai didi

java - 导出到 .jar 后无法解析内容(需要 XML)

转载 作者:行者123 更新时间:2023-12-02 05:49:43 26 4
gpt4 key购买 nike

在我的程序中,我从服务器下载一个 XML 文件作为字符串,并使用证书对其进行签名。当我从 Eclipse 运行该程序时,一切正常。但是当我将其导出到 .jar 文件时,出现以下错误。我应该在哪里寻找问题?

eu.europa.esig.dss.DSSException: Unable to parse content (XML expected)
at eu.europa.esig.dss.DomUtils.buildDOM(DomUtils.java:242)
at eu.europa.esig.dss.DomUtils.buildDOM(DomUtils.java:209)
at eu.europa.esig.dss.xades.signature.EnvelopedSignatureBuilder.buildRootDocumentDom(EnvelopedSignatureBuilder.java:75)
at eu.europa.esig.dss.xades.signature.XAdESSignatureBuilder.build(XAdESSignatureBuilder.java:179)
at eu.europa.esig.dss.xades.signature.XAdESLevelBaselineB.getDataToSign(XAdESLevelBaselineB.java:72)
at eu.europa.esig.dss.xades.signature.XAdESService.getDataToSign(XAdESService.java:92)
at pl.btech.signer.Signer.signFilesWithMSCAPI(Signer.java:116)
at pl.btech.signer.Signer.signXML(Signer.java:58)
at pl.btech.signer.GuiController$1.run(GuiController.java:124)
Caused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipChar(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at eu.europa.esig.dss.DomUtils.buildDOM(DomUtils.java:240)
... 8 more

编辑:这是发生错误的代码片段。内容是我从服务器获取的 XML 字符串。

File srcFile = File.createTempFile("src", ".xml");
FileWriter writer = new FileWriter(srcFile);
writer.write(content);
writer.close();

DSSDocument doc = new FileDocument(srcFile));
ToBeSigned dataToSign = service.getDataToSign(doc, parameters);

最佳答案

FileWriter 使用平台的默认字符集。这因应用程序部署而异,因此这是不可行的。如果 XML 始终采用 UTF-8,请执行以下操作:

Path srcFile = Files.createTempFile("src", ".xml");
Files.write(srcFile, content.getBytes(StandardCharsets.UTF_8));
// Or:
// Files.write(srcFile, Collections.singletonList(content));

避免使用 FileWriter/FileReader。

关于java - 导出到 .jar 后无法解析内容(需要 XML),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56060602/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com