使用apache fop和springboot生成pdf。 Pdf具有从资源下的/ images文件夹加载的图像。在本地eclipse设置中,此方法工作正常,但一旦打包为jar,就无法解析路径
FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
FOUserAgent foUserAgent = fopFactory.newFOUs
erAgent();
ByteArrayOutputStream outputStream = new
ByteArrayOutputStream();
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,
foUserAgent, outputStream);
TransformerFactory factory =
TransformerFactory.newInstance(XSL_LOADER, null);
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
Transformer transformer = factory.newTransformer(new
StreamSource(getClass().getResourceAsStream(XSL_PATH_ROOT)));
解析路径的方法:
private String getImagePath(String icon) {
if (icon != null) {
try {
URL resourceURL = ResourceUtils.getURL(
new StringBuilder().append("classpath:images/").append(icon).append(PNG).toString());
if (ResourceUtils.isJarURL(resourceURL)) {
URL extractedJarURL = ResourceUtils.extractJarFileURL(resourceURL);
return extractedJarURL.getPath();
} else {
return resourceURL.getPath();
}
} catch (FileNotFoundException | MalformedURLException e) {
e.getStackTrace();
}
}
return null;
}
XSL代码:
<xsl:attribute name="src"><xsl:value-of select="iconName" /></xsl:attribute>
以错误org.apache.fop.apps.FOUserAgent获取错误-找不到图像。 URI:文件:/usr/tomcat/banking-app/banking-app.jar!/ BOOT-INF / classes!/images/ICON1.png。 (没有可用的上下文信息)