从Spring Boot中的resources文件夹中读取文件 [英] Read file from resources folder in Spring Boot

查看:1601
本文介绍了从Spring Boot中的resources文件夹中读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot和 json-schema-validator 。我正在尝试从 resources 文件夹中读取名为 jsonschema.json 的文件。我尝试了几种不同的方法,但我无法让它发挥作用。这是我的代码。

  ClassLoader classLoader = getClass()。getClassLoader(); 
文件file = new File(classLoader.getResource(jsonschema.json)。getFile());
JsonNode mySchema = JsonLoader.fromFile(file);

这是文件的位置。





在这里,我可以在类中看到该文件文件夹。





但是当我运行代码时我收到以下错误。

  jsonSchemaValidator错误:java.io.FileNotFoundException:/ home / user / Dev / Java / Java% 20Programs / SystemRoutines / target / classes / jsonschema.json(没有这样的文件或目录)

这是什么我的代码出错了?

解决方案

花了很多时间试图解决这个问题后,终于找到了解决方案这样可行。该解决方案使用了Spring的ResourceUtils。
也适用于json文件。



感谢Lokesh Gupta精心编写的页面:

  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属性fetchProperties(){
属性properties = new Properties();
try {
文件file = ResourceUtils.getFile(classpath:application.properties);
InputStream in = new FileInputStream(file);
properties.load(in);
} catch(IOException e){
LOGGER.error(e.getMessage());
}
返回属性;
}
}


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;
    }
}

这篇关于从Spring Boot中的resources文件夹中读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆