[英]Unable to read file from spring boot .jar
I want to access file on my classpath called reports/invoiceSweetChoice.jasper
in jar on production server.我想访问生产服务器上 jar 中名为reports/invoiceSweetChoice.jasper
类路径上的文件。 Whatever I do I get null.无论我做什么,我都会得到空值。
I have tried this.getClass().getResourceAsStream("reports/invoiceSweetChoice.jasper")
, tried via InputStream
etc. I have printed out content of System.getProperty("java.class.path")
and it is empty.我已经尝试过this.getClass().getResourceAsStream("reports/invoiceSweetChoice.jasper")
,通过InputStream
等尝试过。我已经打印出System.getProperty("java.class.path")
,它是空的。 Not sure how is that possible.不知道这怎么可能。 Do you have any suggestion how to resolve this ?你有什么建议如何解决这个问题吗?
@Autowired private ResourceLoader resLoad;
void someMethod() {
Resource r = resLoad.getResource("classpath:reports/file.abc");
r.getInputStream()...
...
In manifest.mf, the classpath is defined using the class-path key and a space-delimited list of files , as follows:在 manifest.mf 中,类路径是使用class-path 键和空格分隔的文件列表定义的,如下所示:
MANIFEST.MF at root of jarfile. jarfile 根目录下的 MANIFEST.MF。
Class-Path: hd1.jar path/to/label007.jar path/to/foo.jar
If there are spaces in the jar filename, you should enclose them in quotes.如果 jar 文件名中有空格,则应将它们括在引号中。
If it's a webapp, the reports path should be in your BOOT-INF subdirectory of your classpath -- this is automatically performed by maven if you put it in src/main/resources in the standard layout.如果它是一个 web 应用程序,报告路径应该在你的类路径的 BOOT-INF 子目录中——如果你把它放在标准布局的 src/main/resources 中,这将由 maven 自动执行。
EDIT:编辑:
Now that you've clarified what you're trying to do, you have 2 approaches.现在您已经阐明了您要做什么,您有两种方法。 Like I said above, you can grab the file from the BOOT-INF subdirectory of your webapp or you can enumerate the entries in the jar until you find the one you want:就像我上面说的,您可以从 web 应用程序的 BOOT-INF 子目录中获取文件,或者您可以枚举 jar 中的条目,直到找到您想要的条目:
JarInputStream is = new JarInputStream(new FileInputStream("your/jar/file.jar"));
JarEntry obj = null
while ((obj = is.getNextJarEntry()) != null) {
JarEntry entry = (JarEntry)obj;
if (entry.getName().equals("file.abc")) {
ByteArrayOutputStream baos = new ByetArrayOutputStream();
IOUtils.copy(jarFile.getInputStream(entry), baos);
String contents = new String(baos.toByteArray(), "utf-8");
// your entry is now read into contents
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.