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();
    }

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

时间: 2025-01-09 08:11:29

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

eclipse中的js文件报错的解决办法

在使用别人的项目的时候,导入到eclipse中发现js文件报错,解决办法是关闭eclipse的js校验功能. 三个步骤: 1. Eclipse代码   右键点击项目->properties->Validation->Errors/Warming 将Enable Javascript Sematic validation前面的钩子去掉 2.打开.project 文件下面代码去掉 Eclipse代码   <buildCommand> <name>org.eclipse

python 读取位于包中的数据文件

假设你的包中的文件组织成如下: mypackage/ __init__.py somedata.dat spam.py 现在假设spam.py文件需要读取somedata.dat文件中的内容.你可以用以下代码来完成: # spam.py import pkgutil data = pkgutil.get_data(__package__, 'somedata.dat') 由此产生的变量是包含该文件的原始内容的字节字符串. 原文地址:https://www.cnblogs.com/sea-stre

vue3.0在main.js中引入.scss文件报错

写入.scss文件在mian.js中 import './styles/index.scss'后出现上图报错 解决方案: 在vue.config.js文件中添加以下代码 module.exports = { css: { loaderOptions: { sass: { data: `@import "@/style/index.scss";` } } } } 原文地址:https://www.cnblogs.com/jmwei/p/10196554.html

html或者jsp页面引用jar包中的js文件

一,页面上引用jar包中的js文件的方法 使用java web框架AppFuse的时候发现,jquery.bootstrap等js框架都封装到jar包里面了.这些js文件通过一个wro4j的工具对其进行了压缩集成到一个js文件里面.页面使用的时候的时候,引用这一个js文件即可.通过解读wro对于js处理的过程,找到了html或者jsp引用jar包中的js文件的方法: <c:set var="base" value="${pageContext.request.conte

log4j的1.2.15版本,在pom.xml中的顶层project报错错误: Failure to transfer javax.jms:jms:jar:1.1 from https://maven-repository.dev.java.net/nonav/repository......

在动态网站工程中,添加了Pom依赖,当添加log4j的1.2.15版本依赖时,在pom.xml中的顶层project报错错误: Failure to transfer javax.jms:jms:jar:1.1 from https://maven-repository.dev.java.net/nonav/repository......,如下图 这是因为 https://maven-repository.dev.java.net/nonav/repository 这个域名已经无法解析了. 而

NSBundle 读取资源包中的文件

访问项目中资源包里面所有资源使用方法.读取资源包descs.plist文件方法如下: NSBundle *bundle = [NSBundle mainBundle]; //创建bundle对象 NSString *path = [bundle pathForResource:@"descs" ofType:@"plist"]; //获取资源在机器安装后的系统路径 NSArray *allDescs = [NSArray arrayWithContentsofFil

ServletContext读取Web应用中的资源文件

1 package cn.itcast; 2 3 import java.io.FileInputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.PrintWriter; 7 import java.util.Properties; 8 9 import javax.servlet.ServletContext; 10 import javax.servlet.Servlet

mysql安装原码包中的各个文件说明

build 用来制作各个平台二进制的的版本(里面的文件脚本check-cup 可以自动检测) client 各个客户端程序的(mysql.cc mysqladmin.cc...) Docs 文档目录 mysql安装原码包中的各个文件说明

Python包中 __init__.py文件的作用

原创连接 https://www.cnblogs.com/AlwinXu/p/5598543.html Python包中 __init__.py文件的作用 在创建python包的过程中,IDE都会在包根目录下创建一个__init__.py文件,该Python文件默认是空的.目录结构如下: Pycharm下的package树结构: 在Finder中的目录结构: 从Finder中的目录就可以看出来,每个package实际上是一个目录(Directory),那么IDE是怎么识别它为package呢?没