参考:
https://blog.youkuaiyun.com/u013084266/article/details/112238267
https://blog.youkuaiyun.com/qq_18748427/article/details/78606432
现象:
springboot项目读文件流进行数据清洗,本地环境可以读取到数据源,部署到test环境和st环境则报错读不到数据源:cannot be resolved to absolute file path because it does not reside in the file system: jar

原因:
打包之后,spring没办法通过File的形式访问jar包里面的文件。
解决方式1:
使用resource.getInputStream()读取文件内容。
报错代码:
public void dataUpdate2() {
Long startTime = System.currentTimeMillis();
String fileName = "mbk_ctr_usr_detail-2.txt";
int i=0;
int j=0;
int k=0;
int h=0;
try {
File file = ResourceUtils.getFile("classpath:" + fileName);
if (file.isFile() && file.exists()) {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
BufferedReader br = new BufferedReader(isr);
String lineTxt = null;

在SpringBoot项目中,本地环境能正常读取文件流进行数据清洗,但在test和st环境部署时因文件位于jar包内导致无法读取。问题源于Spring无法以File形式访问jar内的文件。解决方案包括使用resource.getInputStream()读取文件内容,或者改变打包方式为tar包并部署时解压。主要代码修改涉及将文件流读取替换为资源流读取。
最低0.47元/天 解锁文章
1598

被折叠的 条评论
为什么被折叠?



