简体   繁体   English

从WEB-INF / lib中的Jar中的类访问tomcat Web应用程序中的文件

[英]Access Files in tomcat web-app from a Class in Jar inside WEB-INF/lib

I have Classes that Generates jasper-reports i am using 我有会生成我正在使用的jasper报告的类

private String RESOURCE_HOME = "/reports/jasper";
getClass().getResource(RESOURCE_HOME + "/srPaySlipSalaryComp.jasper").getFile();

I package this class in a jar and put into my application on tomcat @(WEB-INF/lib). 我将此类包装在一个罐子中,然后放入tomcat @(WEB-INF / lib)上的应用程序中。

Now on my server i have the jaspers in [tomcat]/webapp/[myapp]/reports/jasper 现在在我的服务器上,我在[tomcat] / webapp / [myapp] / reports / jasper中遇到了碧玉

How can i access these files from the Jar? 如何从Jar中访问这些文件?

You access a resource in web application using 您使用来访问Web应用程序中的资源

ServletContext.getResourceAsStream(RESOURCE_HOME + "/srPaySlipSalaryComp.jasper");

The codes above will look from the following locations: 上面的代码将从以下位置查找:

  • web application folder Web应用程序文件夹
  • in /META-INF/resources of each library present in WEB-INF/lib /META-INF/resources WEB-INF/lib存在的每个库的/META-INF/resources

If you want to use the code you have 如果您想使用代码

private String RESOURCE_HOME = "/reports/jasper";
getClass().getResource(RESOURCE_HOME + "/srPaySlipSalaryComp.jasper").getFile();

the file should be present in the following two locations: 该文件应位于以下两个位置:

  • WEB-INF/classes
  • in any library in WEB-INF/lib WEB-INF/lib中的任何库中

getClass().getResource() loads a resource using the class loader. getClass().getResource()使用类加载器加载资源。 So the resources it can load are resources that are accessible in the classpath. 因此,它可以加载的资源是可在类路径中访问的资源。 So it can only load from a jar in WEB-INF/lib , or from WEB-INF/classes , or a jar in tomcat's classpath. 因此,只能从WEB-INF/lib的jar或WEB-INF/classes或tomcat的classpath中的jar加载。

One solution (probably the best one) is thus to also put the jasper files in a jar file in WEB-INF/lib , or in a directory under WEB-INF/classes . 因此,一种解决方案(可能是最好的解决方案)是将jasper文件放在WEB-INF/lib中的jar文件中,或WEB-INF/classes下的目录中。

If you really want to put the jasper files in the webapp, then you need to load the resource using ServletContext.getResource() . 如果您确实要将jasper文件放入webapp,则需要使用ServletContext.getResource()加载资源。

In both solutions, what you'll get is not an URL pointing to a file, but an URL to a resource inside a jar or a war. 在这两种解决方案中,您将获得的不是指向文件的URL,而是指向jar或war中的资源的URL。 So you shouldn't call getFile() . 因此,您不应调用getFile() Pass the URL, or the InputStream returned by getResourceAsStream() , to the Jasper API. 将URL或getResourceAsStream()返回的InputStream传递给Jasper API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 tomcat / lib或WEB-INF / lib中不需要的jar文件 - Unwanted jar files in tomcat/lib or WEB-INF/lib 无法识别web-inf / lib中的jar文件 - jar files in web-inf/lib not recognized 如何解决依赖于 WEB-INF/lib 中已有类的插件所需的 Tomcat Web 应用程序中的 JAR 冲突? - How to solve a JAR conflict in Tomcat web app required for a plugin that has a dependency on a class already in WEB-INF/lib? 可以从WEB-INF \\ lib \\ {*。jar} \\ META-INF \\ resources \\ WEB-INF目录访问tld文件吗? - Can tld files be accessed from WEB-INF\lib\{*.jar}\META-INF\resources\WEB-INF directory? WEB-INF / lib中的Tomcat自定义库jar文件 - Tomcat custom library jar file in WEB-INF/lib 从WEB-INF / lib而不是tomcat / lib加载JDBCRealm驱动程序jar - Loading JDBCRealm driver jar from WEB-INF/lib instead of tomcat/lib 在eclipse中运行tomcat时,不会从WEB-INF / lib中的jar文件加载spring web应用程序上下文 - spring web application context is not loaded from jar file in WEB-INF/lib when running tomcat in eclipse 从部署的 jar 访问 web-inf - Access to web-inf from deployed jar Tomcat无法访问WEB-INF / lib中我的库引用的jar库 - Tomcat can't access jar library referenced by my library in WEB-INF/lib 为什么在WEB-INF / lib中更改jar文件的目录顺序会导致Tomcat 8中出现NoClassDefFoundError? - Why would changing directory order of jar files in WEB-INF/lib cause a NoClassDefFoundError in Tomcat 8?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM