[SpringBoot] 配置文件 与常用方法
配置文件
配置访问前缀
SpringBoot版本 | 配置 |
---|---|
1.x | server.context-path=/api/guache |
2.x | server.servlet.context-path=/api/guache |
yaml:
server:
port: 8201
tomcat:
uri-encoding: UTF-8
remote-ip-header: x-forward-for
max-threads: 1000
max-http-post-size: 102400000 # 设定Httppost数据大小
max-http-header-size: 102400000 # 设定HttpHeader请求头大小ostSize =102400000 //设定Httppost数据大小
servlet:
context-path: /api/guache
访问时即:
http://47.111.170.xx/api/guache
常用方法
读取配置文件
有些读取配置文件的方式,在本机开发调试过程中是正确的,但是在打包为一个jar执行后,就失败了
失败的方式:
file = new ClassPathResource("excel/contract.xlsx").getFile()
java.io.FileNotFoundException: class path resource [excel/contract.xlsx] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/home/project/guache/guache_java/guache.jar!/BOOT-INF/classes!/excel/contract.xlsx
问题的本质不在于某些类的读文件的方式行不行,本质在于,当打包后,是无法获取真实的资源文件的,因为文件在jar中,无法通过路径获取
所以,只能获取到流,而我们实际上想要操作文件,也是通过流操作的
正解为:
InputStream resourceAsStream = ContractFormServiceImpl.class.getResourceAsStream("excel/contract.xlsx");
is = new ClassPathResource("excel/contract.xlsx").getInputStream();
InputStream a = this.getClass().getResourceAsStream("a");
``
本作品采用《CC 协议》,转载必须注明作者和本文链接