Spring Boot - 使用ResourceLoader读取文本文件

9 浏览
0 Comments

Spring Boot - 使用ResourceLoader读取文本文件

我正在尝试使用Spring资源加载器读取文本文件,代码如下:

Resource resource = resourceLoader.getResource("classpath:\\static\\Sample.txt");

该文件位于我的Spring Boot项目中的这个位置:

enter image description here

在Eclipse中运行应用程序时,它能正常工作,但是当我打包应用程序后使用java -jar运行时,我会得到文件找不到的异常:

java.io.FileNotFoundException: class path resource [static/Sample.txt] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/workspace-test/XXX/target/XXX-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/static/Sample.txt

我解压了Jar文件,Sample.txt位于以下位置:

XXX-0.0.1-SNAPSHOT\BOOT-INF\classes\static\Sample.txt

请问有人可以帮助我吗?提前感谢!

0
0 Comments

Spring Boot - 使用ResourceLoader读取文本文件

在使用Spring Boot时,我遇到了同样的问题。正如Lake所解释的那样,使用Spring Boot时,需要将文件加载为InputStream。下面我将添加一个示例代码,其中我想要读取import.xml文件。

public void init() {
    Resource resource = new ClassPathResource("imports/imports.xml");
    try {
        InputStream dbAsStream = resource.getInputStream();
        try {
            document = readXml(dbAsStream);
        } catch (SAXException e) {
            trace.error(e.getMessage(), e);
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            trace.error(e.getMessage(), e);
            e.printStackTrace();
        }
    } catch (IOException e) {
        trace.error(e.getMessage(), e);
        e.printStackTrace();
    }
    initListeImports();
    initNewImports();
}
public static Document readXml(InputStream is) throws SAXException, IOException,
      ParserConfigurationException {
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      dbf.setValidating(false);
      dbf.setIgnoringComments(false);
      dbf.setIgnoringElementContentWhitespace(true);
      dbf.setNamespaceAware(true);
      DocumentBuilder db = null;
      db = dbf.newDocumentBuilder();
      return db.parse(is);
}

我将"imports.xml"添加到了`src/main/resources/imports`目录下。

问题出现的原因是,使用Spring Boot时,需要使用ResourceLoader来加载文件。而解决方法是使用ResourceLoader的实现类ClassPathResource来获取InputStream,然后通过InputStream来读取文件内容。

以上就是使用Spring Boot中使用ResourceLoader读取文本文件的解决方法。

0
0 Comments

Spring Boot是一个用于创建独立的、生产级别的Spring应用程序的框架。在使用Spring Boot时,有一个常见的问题是如何使用ResourceLoader读取文本文件。下面我们来看一下这个问题出现的原因以及解决方法。

问题的原因是,如果我们想要从Spring Boot JAR的classpath中加载一个文件,我们必须使用`resource.getInputStream()`而不是`resource.getFile()`。如果我们尝试使用`resource.getFile()`,就会收到一个错误,因为Spring试图访问一个文件系统路径,但它无法访问JAR中的路径。

解决这个问题的方法是使用`resource.getInputStream()`来获取文件的输入流。通过使用输入流,我们可以读取文件的内容而不需要访问文件系统路径。下面是解决方法的代码示例:

@Autowired
private ResourceLoader resourceLoader;
public String readFileFromClasspath(String path) throws IOException {
    Resource resource = resourceLoader.getResource("classpath:" + path);
    InputStream inputStream = resource.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    StringBuilder result = new StringBuilder();
    String line;
    while ((line = reader.readLine()) != null) {
        result.append(line);
    }
    reader.close();
    return result.toString();
}

在上面的代码中,我们首先通过`resourceLoader.getResource("classpath:" + path)`来获取资源。然后,我们使用`resource.getInputStream()`来获取文件的输入流。接下来,我们使用输入流创建一个`BufferedReader`对象,并通过逐行读取文件内容来构建一个`StringBuilder`对象。最后,我们关闭读取器并将文件内容以字符串的形式返回。

通过以上的解决方法,我们可以在Spring Boot应用程序中使用ResourceLoader来读取classpath中的文本文件,而不需要访问文件系统路径。这样可以让我们更方便地处理文件操作,并且提高了应用程序的可移植性和灵活性。

参考链接:[https://smarterco.de/java-load-file-classpath-spring-boot/](https://smarterco.de/java-load-file-classpath-spring-boot/)

0
0 Comments

(Spring Boot - 读取文本文件使用ResourceLoader)

在使用 "java -jar XXXX.jar" 运行时,请尝试以下代码:

resourceLoader.getResource("classpath:static/Sample.txt");

在经过你的代码后,问题是你尝试使用 `FileInputStream` 读取文件,但实际上它在jar文件内部。

但是实际上你得到了 `org.springframework.core.io.Resource`,这意味着你可以得到InputStream,所以你可以这样做:

new BufferedReader(new InputStreamReader(resource.getInputStream())).readLine();

对我不起作用,可能是因为我正在使用Spring Boot?

是的,我正在使用spring boot 1.4.3,你的确切版本是什么?

我的版本是1.4.3.RELEASE,这很奇怪!

这是一个干净的Spring Boot项目吗?你改过Spring Boot配置了吗?也许你需要检查MANIFEST.MF文件中的 `Spring-Boot-Classes`,像这样:`Spring-Boot-Classes: BOOT-INF/classes/`

是的,这是一个全新的项目,所有配置都是默认的。

有趣,也许你可以将它推到GitHub上,这样我就可以获取你的代码并尝试一下。

这是一个简单的项目,一个控制器和一个服务类。

好的,真正的问题是由 `try(Scanner sc = new Scanner(new FileInputStream(resource.getFile())))` 引起的,实际上 `resourceLoader.getResource("classpath:static/Sample.txt")` 获取了真正的资源。

让我们在聊天中继续讨论

0