春季启动-无法从资源(jar)加载文件。...在_some_线程上

时间:2020-08-26 15:02:16

标签: spring-boot jar resources classpath

我有一个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;
}

您知道我该如何解决吗?

0 个答案:

没有答案