欢迎光临
我们一直在努力

Spring Boot如何从classpath下读取文件(jar打包/war打包)

在创建Spring Boot Web应用时,有时需要从classpath加载文件。下面是在Jar包/war包中如何读取文件的解决方案:

从war包中读取

方法1:

 public static File readByFile(String relativePath) {
        File file = null;
        try {
            file = ResourceUtils.getFile("classpath:" + relativePath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return file;
    }

方法2:

参考: Java获取路径时Class.getResource()和ClassLoader.getResource()区别

从jar包中读取

方法1

Resource resource = new ClassPathResource("/" + relativePath);
        InputStream fileInputStream = null;
        try {
            fileInputStream = resource.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

方法2

使用org.springframework.core.io.ResourceLoader

@Service("geolocationservice")
public class GeoLocationServiceImpl implements GeoLocationService {

    private static final Logger LOGGER = LoggerFactory.getLogger(GeoLocationServiceImpl.class);

    private static DatabaseReader reader = null;
    private ResourceLoader resourceLoader;

    @Inject
    public GeoLocationServiceImpl(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @PostConstruct
    public void init() {
        try {
            LOGGER.info("GeoLocationServiceImpl: Trying to load GeoLite2-Country database...");

            Resource resource = resourceLoader.getResource("classpath:GeoLite2-Country.mmdb");
            InputStream dbAsStream = resource.getInputStream(); // <-- this is the difference

            // Initialize the reader
            reader = new DatabaseReader
                        .Builder(dbAsStream)
                        .fileMode(Reader.FileMode.MEMORY)
                        .build();

            LOGGER.info("GeoLocationServiceImpl: Database was loaded successfully.");

        } catch (IOException | NullPointerException e) {
            LOGGER.error("Database reader cound not be initialized. ", e);
        }
    }

    @PreDestroy
    public void preDestroy() {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                LOGGER.error("Failed to close the reader.");
            }
        }
    }
}

参考:

  1. Java: Load file from classpath in Spring Boot
  2. Spring Boot – Reading Text File using ResourceLoader
  3. Java获取路径时Class.getResource()和ClassLoader.getResource()区别
  4. org.springframework.core.io.ResourceLoader
赞(0) 打赏
未经允许不得转载:Ddmit » Spring Boot如何从classpath下读取文件(jar打包/war打包)

评论 抢沙发

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

登录

找回密码

注册