zoukankan      html  css  js  c++  java
  • springboot jar启动 读取jar包中相对路径文件报错

    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();
        }
  • 相关阅读:
    hdu 1087(LIS变形)
    poj 1088(记忆化搜索)
    hdu 1505(最大子矩阵)
    hdu 1506(好题+DP或者RMQ)
    poj 2593&&poj2479(最大两子段和)
    hdu 1003(最大子段和)
    hdu 2881(LIS变形)
    poj 1692(动态规划)
    CodeForces 626C Block Towers
    CodeForces 626B Cards
  • 原文地址:https://www.cnblogs.com/paisen/p/11719402.html
Copyright © 2011-2022 走看看