SpringBoot项目打包部署,读取jar里面的文件报错500,异常日志关键提示

cannot be resolved to absolute file path because it does not reside in the file system

报错定位

 is = new FileInputStream(ResourceUtils.getFile("classpath:static/data_template/biz/export_XXXX_report.xlsx"));

从报错可看到读取文件路径****XXX.jar!******.xlsx,XXX.jar就是打成jar,里面包含了需要读取的模板文件

具体Spring core(版本5.2.12)包源码ResourceUtils

public static File getFile(URL resourceUrl, String description) throws FileNotFoundException {Assert.notNull(resourceUrl, "Resource URL must not be null");if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) {//报错所在地方throw new FileNotFoundException(description + " cannot be resolved to absolute file path " +"because it does not reside in the file system: " + resourceUrl);}try {return new File(toURI(resourceUrl).getSchemeSpecificPart());}catch (URISyntaxException ex) {// Fallback for URLs that are not valid URIs (should hardly ever happen).return new File(resourceUrl.getFile());}}

解决办法如下:

//获得文件流时,因为读取的文件是在打好jar文件里面,不能直接通过文件资源路径拿到文件,但是可以在jar包中拿到文件流
//ResourcePatternResolver的实现方法,可以匹配到各种部署时的各种文件类型例如war,jar,zip等等findPathMatchingResources
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("static/data_template/biz/XXXX.xlsx");
Resource resource = resources[0];
InputStream  is = resource.getInputStream();

Spring源码(PathMatchingResourcePatternResolver)关键代码段

@Overridepublic Resource[] getResources(String locationPattern) throws IOException {Assert.notNull(locationPattern, "Location pattern must not be null");if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {// a class path resource (multiple resources for same name possible)if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) {// a class path resource patternreturn findPathMatchingResources(locationPattern);}else {// all class path resources with the given namereturn findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));}}else {// Generally only look for a pattern after a prefix here,// and on Tomcat only after the "*/" separator for its "war:" protocol.int prefixEnd = (locationPattern.startsWith("war:") ? locationPattern.indexOf("*/") + 1 :locationPattern.indexOf(':') + 1);if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) {// a file patternreturn findPathMatchingResources(locationPattern);}else {// a single resource with the given namereturn new Resource[] {getResourceLoader().getResource(locationPattern)};}}}

发布到服务器,问题解决!!

导出报错cannot be resolved to absolute file path because it does not reside in the file system相关推荐

  1. 问题备忘: class path resource [xx] cannot be resolved to absolute file path because it does not reside

    问题描述 测试服务的版本是Spring Cloud Dalston.SR5 在Spring Boot中配置https时,代码如下: @Bean@ConditionalOnExpression(&quo ...

  2. oracle导出报错04063,导出报错:ORA-04063:packagebody“DMSYS.DBMS_DM_MODEL_EXP”hase

    导出报错:ORA-04063: package body DMSYS.DBMS_DM_MODEL_EXP has errors Oracle 11.2.0.2 , EBS R12.1.3 因为是升级的 ...

  3. 11g导出报错:EXP-00106: Invalid Database Link Passwords

    11g导出报错:EXP-00106: Invalid Database Link Passwords 服务器端:11.2.0.4.0 : 导出客户端:11.2.0.2 报错提示: About to e ...

  4. 【PR】用PR剪辑影片后导出报错:Adobe media Encoder未安装

    用PR剪辑影片后导出报错:Adobe media Encoder未安装,这里提供PR CC 2017对应的 Media Encoder安装包,安装后即可正常导出 多次尝试 无法上传附件,C站对资源大小 ...

  5. Vue项目 报错TypeError [ERR INVALID ARG TYPE]: The “path“ argument must be of type string

    # Vue项目 报错TypeError [ERR INVALID ARG TYPE]: The "path" argument must be of type string 卡了半 ...

  6. CentOS7安装twisted报错: src/twisted/test/raiser.c:4:20: fatal error: Python.h : No such file or direc

    问题: CentOS7安装twisted报错: src/twisted/test/raiser.c:4:20: fatal error: Python.h : No such file or dire ...

  7. oracle导出数据库中表出现导出报错(EXP-00003)未找到段 (0,0) 的存储定义

    前两天在使用oracle数据库,使用dba用户利用PL/SQL 中的tool中export table导出表结构时,出现了如下问题: 好多张表"报错(EXP-00003)未找到段 (0,0) ...

  8. oracle导出报错04063,Oracle EXP导出报错的解决方法

    前段时间上海某保险客户IT主管打电话过来,说他们的开发人员在开发环境中执行EXP报错,让我过去检查下.本着"客户为本,服务为根"的宗旨,第一时间赶到客户现场. 和客户开发人员沟通下 ...

  9. 使用exp导出报错EXP-00091

    用exp导出数据时出现如下报错信息: 1)查询数据库的字符集 select userenv('language') from dual; 2)方案一: 设置Linux操作系统的NLS_LANG环境变量 ...

最新文章

  1. 五年循环期限已到,我们又要步入“AI寒冬”了吗?
  2. Java中isAssignableFrom的用法
  3. 一个成熟的网站模板如何引起用户的注意?
  4. Yet Another Multiple Problem 同余定理 bfs
  5. Effective Modern C++:06lambda表达式
  6. 【报告分享】2021全球职场调研中国报告:期待与忐忑,职场人的心声-普华永道.pdf(附下载链接)...
  7. 框架原理第三讲,RTTCreate,运行时类型创建.(以MFC框架讲解)
  8. OpenGL ES着色器语言之变量和数据类型(一)(官方文档第四章)和varying,uniform,attribute修饰范围...
  9. oracle with as用法_关于Oracle with语句用法说明
  10. 基于贝叶斯分类的中文人名用字特征的性别识别
  11. svn 回退/更新/取消至某个版本命令详解
  12. 经纬财富:四平怎么炒白银能挣到钱?
  13. git clone下载代码,中途断掉怎么办?
  14. 关于在VMware上安装Android x86及FTP详细使用
  15. CUDA流多处理器(stream multiprocessor,sm)和硬件流处理器(stream processor,sp)
  16. 写不完的数学试卷-----试卷生成器(Qt含源码)
  17. 立大志为什么得不了中志? 我们缺少了合理期望
  18. 豆瓣上《特权和寻租的经济学》的书评
  19. RedHat Linux 9.0 安装教程(全程图解)
  20. mac上安装windows系统

热门文章

  1. clean后class文件全部丢失_大数据专家,详解HadoopMapReduce处理海量小文件:压缩文件
  2. CrossOver介绍
  3. 版权“陈冠希”或将断送网络视频前景
  4. VR Infinite Gesture使用教程
  5. argmax函数_Python之Numpy库常用函数合集(附注释)
  6. ansible剧本如何写_我学过的3课:写Ansible剧本
  7. 《Win32多线程程序设计》-侯杰【最后更新日期:16/02/23
  8. 解决Cross origin requests are only supported for protocol schemes问题
  9. RHCE红帽认证工程师
  10. Oracle 11G OCP 1Z0-053 676