我有一个spring-boot应用程序。如果请求从RestController终结点方法读取文件,则可以加载资源(在app jar中)中的文件,而不会出现任何问题。
但是,还有一条执行路径正在使用连接到RestController的另一个线程 not 。从那个线程,我得到一个例外:
java.io.FileNotFoundException: class path resource [path-in-jar] cannot be resolved to URL because it does not exist
我正在像这样从罐子中读取文件:
public String getContent(String resourcePath) {
ClassPathResource cl = new ClassPathResource(resourcePath);
URL url = cl.getURL(); // <-- exception is raised over here
String content;
try (InputStream in = url.openStream()) {
content = new String(in.readAllBytes(), StandardCharsets.UTF_8);
}
return content;
}
您知道我该如何解决吗?