【发布时间】:2019-03-26 08:03:36
【问题描述】:
在我的网络应用程序中,我想读取 src/main/resource/static/doc 中的 excel 文件,所以我使用了
String basePath = ClassLoader.getSystemResource("").getPath();
获取资源路径并读取它。它在我的 IDE IDEA 中运行,我只需运行它即可获得 excel。
但是,当我使用java -jar lab.jar 运行spring boot 项目时,它抛出了NullpointerException,但是当我使用时
String basePath = ClassLoader.getSystemResource("application.properties").getPath();
打印出来,我看到了/Users/zhangzhikai/lab-center/target/lab.jar!/BOOT-INF/classes!/application.properties。
为什么我无法从 jar 中获取 excel?
【问题讨论】:
-
直接使用这个 File file = new ClassPathResource("/doc/modal/model.xls").getFile();
-
因为打包成JAR就没有这种东西了。 @this_is_om_vm 的解决方案也不起作用,因为在打包的应用程序中没有指向该资源的
File。相反,只需使用ClassPathResource来读取InputStream而不是File。 -
@this_is_om_vm 我想读取 modal.xls 并创建新文件 new.xls 到 doc/temp,我可以获取 doc 路径吗?
-
@M.Deinum 谢谢你的回复。你能帮我写代码吗?我很不明白。
标签: java spring-boot jar