spring boot项目打成的 jar包无法获取src/main/resources下文件

jhnm 发布于 2017/11/13 19:01
阅读 38K+
收藏 3

解锁HarmonyOS核心技能,赢取限量周边好礼>>>

@Component
public class ResourceConfig {

    public JsonObject orgsJson ;
    public JsonObject chaincodeJson ;

    @Value("classpath:orgs.json")
    private Resource orgs;

    @Value("classpath:chaincode.json")
    private Resource ccs;

    @PostConstruct
    public void init(){
        System.out.println("--------init---------");
        try {
            orgsJson = new JsonParser().parse(new FileReader(orgs.getFile())).getAsJsonObject();
            chaincodeJson = new JsonParser().parse(new FileReader(ccs.getFile())).getAsJsonObject();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

有一个获取src/main/resources下文件的代码如上:
在Intellij Idea中直接run 正常启动,但打成jar包以java -jar运行则报异常: 
java.io.FileNotFoundException: class path resource [orgs.json] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/ideaSpace/fabric-poc/target/fabric-poc.jar!/BOOT-INF/classes!/orgs.json
这是什么原因呢,如何解决? 

加载中
0
买房也用券
买房也用券

楼主,解决了没,我也遇到这个问题了,spring boot 在centos 7.4上打包就出这问题,代码在win,centos7.3 打包又没问题。。

0
杨辉_0716

 

上文内答复摘选:::::::

resource.getFile() expects the resource itself to be available on the file system, i.e. it can't be nested inside a jar file. This is why it works when you run your application in STS but doesn't work once you've built your application and run it from the executable jar. Rather than using getFile() to access the resource's contents, I'd recommend using getInputStream() instead. That'll allow you to read the resource's content regardless of where it's located.

几个实现方式:

String data = "";
ClassPathResource cpr = new ClassPathResource("static/file.txt");
try {
    byte[] bdata = FileCopyUtils.copyToByteArray(cpr.getInputStream());
    data = new String(bdata, StandardCharsets.UTF_8);
} catch (IOException e) {
    LOG.warn("IOException", e);
}

场景二:

ClassPathResource classPathResource = new ClassPathResource("static/something.txt");

InputStream inputStream = classPathResource.getInputStream();
File somethingFile = File.createTempFile("test", ".txt");
try {
    FileUtils.copyInputStreamToFile(inputStream, somethingFile);
} finally {
    IOUtils.closeQuietly(inputStream);
}

场景三:

       Resource[] resources;

        try {
            resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath:" + location + "/*.json");
            for (int i = 0; i < resources.length; i++) {
                try {
                InputStream is = resources[i].getInputStream();
                byte[] encoded = IOUtils.toByteArray(is);
                String content = new String(encoded, Charset.forName("UTF-8"));
                }
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }

----------------------------------------------- 变态的分割线 -------------------------------------------------

楼主给的连接中还有很多有营养的回复,请自行查看 

0
j
jhnm
该评论暂时无法显示,详情咨询 QQ 群:912889742
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部