首页 > 解决方案 > 加载外部文件作为资源

问题描述

我正在加载资源文件,如下所示

public static JSONArray readConfig(String fileName) throws IOException, JSONException{
    ClassPathResource res = new ClassPathResource(fileName);    
    byte[] bdata = FileCopyUtils.copyToByteArray(res.getInputStream());
    String json = new String(bdata, StandardCharsets.UTF_8);
    JSONArray jarr = new JSONArray(json);
    return jarr;
};

我想知道是否可以将“/something/something1/config.json”之类的外部位置/文件添加到资源/类路径中,以便可以通过与上面相同的代码读取。

谢谢...

标签: javaspring-boot

解决方案


您可以在类路径中添加文件夹位置。

java -Dloader.path="/something/something1/" -jar your-app.jar

参见文档链接_loader.path

如果要加载不在类路径中的资源,请使用FileSystemResource而不是,ClassPathResource这样它将在文件系统中加载资源。


推荐阅读