gpt4 book ai didi

java - 指定jar内xsl文件的正确路径

转载 作者:行者123 更新时间:2023-12-01 09:10:57 25 4
gpt4 key购买 nike

我有一个 XSL 文件位于包内的 src/main/java 文件夹中:

eu.deniss.report.view.xslt

我用它来获取文件位置:

String location = './src/main/java/eu/deniss/report/view/xslt/'

虽然从 IDE 运行时运行良好,但创建 .jar 时出现错误,无法找到该文件。

当我的应用程序打包为 .jar 时,调用此 xsl 文件的正确路径/方式是什么。此应用程序使用 Spring Boot。

编辑:

这是我加载 xslt 文件的方式:

String xslFileForProcessing = "someFile.xslt";

File xsltFile = new File(xslFileProperties.getLocation() + xslFileForProcessing);

TransformerFactory transformerFactory = new net.sf.saxon.TransformerFactoryImpl();
Source xsltSource = new StreamSource(xsltFile);
Transformer transformer = transformerFactory.newTransformer(xsltSource);

最佳答案

使用InputStream作为StreamSource实例的参数。您将能够轻松加载类路径中可用的资源:

final InputStream xsltStream = getClass().getResourceAsStream("/eu/deniss/report/view/xslt/someFile.xslt");

Source xsltSource = new StreamSource(xsltStream);

// for performance optimizations
Templates cached = factory.newTemplates(xsltSource);
Transformer transformer = cached.newTransformer();

关于java - 指定jar内xsl文件的正确路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40927721/

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