当前位置: 首页 > article >正文

Freemarker模板 jar!/BOOT-INF/classes!/**.html

需求:发送邮件,邮件内容通过Freemaker模板生成,如下代码:

Configuration configuration = new Configuration(Configuration.getVersion());
configuration.setDefaultEncoding("utf-8");
/** 加载模板目录 **/
//这个方法在IDEA跑是OK 的
File file = ResourceUtils.getFile("classpath:templates");//templates为模板目录
configuration.setDirectoryForTemplateLoading(file);
Template template = configuration.getTemplate("test.html");//test.html为要加载的模板

但部署到服务器,却提示:

java.io.FileNotFoundException:file:**.jar!/BOOT-INF/classes!/**.html 

来回找原因,可以看到文件路径出现了奇怪的感叹号!,这导致路径无法被正确匹配。

问题产生原因:当我们使用文件路径访问文件时,该路径下的文件必须是可访问的,而jar文件本质是上是一个压缩文件,需要解压才能访问,所以程序会直接报错。

解决办法:采用类路径加载模板目录的方式。 Freemarker提供了3种加载模板目录的方法详见:Freemarker加载模板目录的方法-CSDN博客

configuration.setClassForTemplateLoading(this.getClass(), "/templates");

configuration.getTemplate("test.html"); //test.html为要装载的模板

http://www.kler.cn/a/399635.html

相关文章:

  • 编译安卓SDK时出现:600:26 test android/soong/ui/build/paths的解决方案
  • Swift 宏(Macro)入门趣谈(二)
  • 【网络安全】记一次APP登录爆破
  • 抖音热门素材去哪找?优质抖音视频素材网站推荐!
  • Flutter网络通信-封装Dio
  • 网络安全:数字时代的护城河
  • 机器学习笔记2 - 机器学习的一般流程
  • Unity-Editor扩展Odin + 自定义EditorWindow记录
  • Python正则表达式中re.M 是什么意思
  • Big Data for AI实践:面向AI大模型开发和应用的大规模数据处理套件
  • 【WPF】Prism学习(四)
  • 深入浅出 Go 语言:现代编程的高效选择
  • 【PGCCC】Postgresql 存储设计
  • Flink运行时架构以及核心概念
  • 基于SpringBoot+Vue的船舶维保管理系统(带1w+文档)
  • UE5的线程同步机制
  • 鸿蒙HarmonyOS 地图定位到当前位置 site查询等操作
  • Rewar Model的输出(不包含训练)
  • <有毒?!> 诺顿检测:这篇 CSDN 文章有病毒
  • Gin 框架中的路由