您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在Spring Boot中从资源文件夹读取文件

在Spring Boot中从资源文件夹读取文件

在花费大量时间尝试解决此问题之后,终于找到了可行的解决方案。该解决方案利用了Spring的ResourceUtils。也应该适用于json文件

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

要回答有关评论的一些问题:

可以肯定地说,我已经在Amazon EC2上使用 java -jar target/image-service-slave-1.0-SNAPSHOT.jar

Java 2022/1/1 18:21:49 有568人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶