当前位置:网站首页>war或jar使用Resource或ClassPathResource加载classpath下文件失败
war或jar使用Resource或ClassPathResource加载classpath下文件失败
2022-07-16 23:25:00 【滴水可藏海】
代码:
String path = "cerm/CA_CA_0_usr_0.der";
File file = new DefaultResourceLoader().getResource(path).getFile();
byte[] bytes = FileUtils.readFileToByteArray(file);
System.out.println(Base64.getEncoder().encodeToString(bytes));异常:
cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/opt/d_g.war!/WEB-INF/classes!/cerm/CA_CA_0_usr_0.der使用idea测试时,未打包时可以获取到文件,打包后报错。
解决:
String path = "cerm/CA_CA_0_usr_0.der";
InputStream inputStream = new DefaultResourceLoader().getResource(path).getInputStream();
//InputStream转为byte[]
byte[] bytes = inputStream2Bytes(inputStream);
System.out.println(Base64.getEncoder().encodeToString(bytes));说明:
这是因为打包后Spring试图访问文件系统路径,但无法访问JAR中的路径。因此必须使用getInputStream()。
并且使用ClassPathResource亦同理。
详情请参考:Java: Load file from classpath in Spring Boot - smarterco.de
边栏推荐
- el-table动态添加可输入行的问题
- 我的創作紀念日
- Coordinate solution of rectangular coordinate system rotation (n*90 °) (1 ≤ n ≤ 4)
- International Conference on Sustainable hydraulic science and green infrastructure 18-19
- Wechat applet development learning 3 (wxss template and global configuration)
- Application of optimistic lock and pessimistic lock in kubernetes
- Golang创建文件权限问题解决方法
- Mysql—锁:全面理解
- 抓包工具哪家强(暴力窃取前戏)
- 【无标题】
猜你喜欢

eTS开发方式三方组件推荐【系列1】

Switch and router technology: link aggregation, spanning tree protocol STP and spanning tree protocol configuration

交换机与路由器技术:热备份路由选择协议HSRP、HSRP和SPVSTP综合实验

NiO three board axe buffer, a double-edged sword to improve program efficiency

Press and hold day 27: Service

What do LETV employees rely on to live a "fairy life" without a boss?

Box model, table case

有的小伙伴问我英语不好可以学编程吗?

uml类图浅录

Shell's third day small exercise: access the self built nextcloud network disk service through the self built DNS server
随机推荐
Vite3.0 发布
Application of optimistic lock and pessimistic lock in kubernetes
API design principles from the requirements of compiler for instruction set
@How can conditionalonmissingbean cover beans in third-party components
Customized serialization protocol, which is flexible, but not universal
Comment conditionalonmissingbean implémente l'écrasement des haricots dans les composants tiers
盛夏防火患!广东开展夏季消防安全检查
Business is too busy. Is there really no reason to have time for automation?
Why processing data indicators
Master-slave separation of database
SAP ABAP excel export (multiple sheet pages)
[Clickhouse] calculation of weeks
【日常训练】剑指 Offer II 041. 滑动窗口的平均值
NXP i.MX8M Plus赋能边缘机器学习,启扬IAC-IMX8MP-Kit开发板
Points needing attention in vertx programming
从编译器对指令集的要求看API设计原则
Daily question 1: palindrome linked list (Sword finger off027)
我的创作纪念日
【自然语言处理】【多模态】多模态综述:视觉语言预训练模型
Using backup to restore the database, but there is no control file. How to solve the file