从 Spring Boot 中的资源文件夹中读取文件 [英] Read file from resources folder in Spring Boot
问题描述
我正在使用 Spring Boot 和 json-schema-validator
.我正在尝试从 resources
文件夹中读取一个名为 jsonschema.json
的文件.我尝试了几种不同的方法,但无法正常工作.这是我的代码.
ClassLoader classLoader = getClass().getClassLoader();File file = new File(classLoader.getResource("jsonschema.json").getFile());JsonNode mySchema = JsonLoader.fromFile(file);
这是文件的位置.
在这里我可以看到 classes
文件夹中的文件.
但是当我运行代码时出现以下错误.
jsonSchemaValidator 错误:java.io.FileNotFoundException:/home/user/Dev/Java/Java%20Programs/SystemRoutines/target/classes/jsonschema.json(没有这样的文件或目录)
我在代码中做错了什么?
在花了很多时间试图解决这个问题之后,终于找到了一个有效的解决方案.该解决方案利用了 Spring 的 ResourceUtils.应该也适用于 json 文件.
感谢 Lokesh Gupta 写得很好的页面:
package utils;导入 org.slf4j.Logger;导入 org.slf4j.LoggerFactory;导入 org.springframework.util.ResourceUtils;导入 java.io.FileInputStream;导入 java.io.IOException;导入 java.io.InputStream;导入 java.util.Properties;导入 java.io.File;公共类实用程序{私有静态最终记录器 LOGGER = LoggerFactory.getLogger(Utils.class.getName());公共静态属性 fetchProperties(){属性 properties = new Properties();尝试 {File file = ResourceUtils.getFile("classpath:application.properties");InputStream in = new FileInputStream(file);properties.load(in);} catch (IOException e) {LOGGER.error(e.getMessage());}返回属性;}}
回答评论中的一些问题:
很确定我使用 java -jar target/image-service-slave-1.0-SNAPSHOT.jar
看看我的 github 存储库:https://github.com/johnsanthosh/image-service找出从 JAR 运行它的正确方法.
I'm using Spring Boot and json-schema-validator
. I'm trying to read a file called jsonschema.json
from the resources
folder. I've tried a few different ways but I can't get it to work. This is my code.
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("jsonschema.json").getFile());
JsonNode mySchema = JsonLoader.fromFile(file);
This is the location of the file.
And here I can see the file in the classes
folder.
But when I run the code I get the following error.
jsonSchemaValidator error: java.io.FileNotFoundException: /home/user/Dev/Java/Java%20Programs/SystemRoutines/target/classes/jsonschema.json (No such file or directory)
What is it I'm doing wrong in my code?
After spending a lot of time trying to resolve this issue, finally found a solution that works. The solution makes use of Spring's ResourceUtils. Should work for json files as well.
Thanks for the well written page by Lokesh Gupta : Blog
package utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.io.File;
public class Utils {
private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class.getName());
public static Properties fetchProperties(){
Properties properties = new Properties();
try {
File file = ResourceUtils.getFile("classpath:application.properties");
InputStream in = new FileInputStream(file);
properties.load(in);
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
return properties;
}
}
To answer a few concerns on the comments :
Pretty sure I had this running on Amazon EC2 using java -jar target/image-service-slave-1.0-SNAPSHOT.jar
Look at my github repo : https://github.com/johnsanthosh/image-service to figure out the right way to run this from a JAR.
这篇关于从 Spring Boot 中的资源文件夹中读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!