首页 > 编程语言 > 详细

springboot jar启动 读取jar包中相对路径文件报错

时间:2019-10-22 14:48:42      阅读:704      评论:0      收藏:0      [点我收藏+]
jar包启动后读取相对路径文件报异常:
Caused by: java.io.FileNotFoundException: class path resource [***.***] cannot be resolved to absolute 
.***.jar/BOOT-INF/classes!/***.**

采用流的方式读取即可解决

// /template/template.html是resource下面的文件
String template=readfile("/template/template.html");
//file 相对路径
 public  String readfile(String file){
        InputStream stream = this.getClass().getResourceAsStream(file);
        StringBuffer sb = new StringBuffer() ;
        BufferedReader br = null ;
        try {
            br = new BufferedReader(new InputStreamReader(stream,"UTF-8")) ;
            String s=null ;
            while((s=br.readLine()) !=null){
                sb.append(s) ;
            }
            br.close();
        } catch (FileNotFoundException e) {
            log.error("FileNotFoundException:"+e);
        } catch (IOException e) {
            log.error("IOException :"+e);
        }finally {
            if(br !=null){
                try {
                    br.close();
                } catch (IOException e) {
                    log.error("close br error:"+e);
                }
            }
        }
        return sb.toString();
    }

 

springboot jar启动 读取jar包中相对路径文件报错

原文:https://www.cnblogs.com/paisen/p/11719402.html

(1)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!