spring-boot引用资源:图片、json文件、模板目录

前端时间,在idea上运行项目OK,但在打成jar包后运行却出了岔子。网上一番搜索,终于得到了解决:使用流,使用类路径

干货

图片,json文件,模板目录

引用图片
// 图片,使用流
// classes  文件名前加了“/”,则表示从类路径下也就是从classes文件夹下查找资源。
System.out.println(.class.getResourceAsStream("/static/images/logo.png")); //该目录是以resources目录为根目录
InputStream inputStream1 = WordUtil.class.getResourceAsStream("/static/images/logo.png");
// 图片,使用流
// package  文件名前不加“/”,则表示从当前类所在的包下查找该资源。
System.out.println(对象.getClass().getResourceAsStream("logo.png"));
InputStream inputStream2 = wordUtil.getClass().getResourceAsStream("logo.png");
// 前提:编译后的文件夹下有该图片资源,如果没有,则需要自行配置资源打包的路径(这个我不会)
// 当然也有野蛮的方式:解压jar包,将图片资源放入指定的位置;或者在编译后的文件夹下放入图片,然在打包。
引用json文件
// json,使用流
String path = "static/js/json/16.json";//该目录是以resources目录为根目录
InputStream is = jarPathDemo.getClass().getClassLoader().getResourceAsStream(path);
BufferedReader in = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
StringBuffer buf = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null){buf.append(line);
}
String input = buf.toString();
jsonArray = JSONArray.fromObject((Object)input);
System.out.println(jsonArray);
引用模板目录
configuration.setClassForTemplateLoading(CreateWordUtil.class, "/templates/simple");//该目录是以resources目录为根目录

请看源代码(生成word)

生成word文档,需要使用到模板目录

导入依赖,一个控制层WordController,一个工具类WordUtil,一个ftl,一个http请求。

包含了 图片文件 + 模板目录

maven依赖
<!--freemarker依赖-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
控制层WordController
package com.word.controller;import com.word.util.WordUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;@RestController
public class WordController {@RequestMapping("word")public void createWord() throws Exception {Map<String, Object> dataMap = new HashMap<String, Object>();dataMap = WordUtil.dataMap(dataMap);WordUtil.createWord(dataMap,"/templates/body","1.ftl","D:/1.doc"); // 该目录是以resources目录为根目录}
}
工具类WordUtil
package com.word.util;import freemarker.template.Configuration;
import freemarker.template.Template;
import sun.misc.BASE64Encoder;import java.io.*;
import java.util.Map;public class WordUtil {/*** @param dataMap         模板动态数据* @param packagePath     模板所在目录* @param ftlName         模板文件名* @param pathName        创建word路径(绝对路径+文件名)* @throws Exception      抛出异常(模板异常,流异常,文件异常)*/public static void createWord(Map<String, Object> dataMap, String packagePath, String ftlName, String pathName) throws Exception {// 指定版本Configuration configuration = new Configuration(/*Configuration.VERSION_2_3_31*/);// 设置编码configuration.setDefaultEncoding("UTF-8");// 指定模板目录configuration.setClassForTemplateLoading(WordUtil.class, packagePath);// 指定模板Template template = configuration.getTemplate(ftlName);// 指定文件Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(pathName)), "UTF-8"));// 整合数据template.process(dataMap, out);// 强制将所有缓冲的输出字节被写出out.flush();// 关闭流out.close();}public static Map<String, Object> dataMap(Map<String, Object> dataMap) {dataMap.put("hospital_name", "xxx医院");dataMap.put("hospital_logo", getImageStr( "/static/images/logo.png"));dataMap.put("scale_name", "xxx量表");return dataMap;}/*** @param imgFile* @return 返回图片字符串*/public static String getImageStr(final String imgFile) {InputStream in = null;byte[] data = null;try {// 使用类路径,使用流,来获取图片文件in = WordUtil.class.getResourceAsStream(imgFile);data = new byte[in.available()];in.read(data);in.close();}catch (FileNotFoundException e) {e.printStackTrace();}catch (IOException e2) {e2.printStackTrace();}final BASE64Encoder encoder = new BASE64Encoder();return encoder.encode(data);}}
ftl模板

三个ftl文件:在同一个目录内

想详细了解ftl语法的需要自行搜索了,

关于ftl文件的生成:新建一个word文档,编辑好内容,以xml格式保存,在idea中重命名:以ftl为后缀

1.ftl

文件位置:resources/templates/body/1.ftl

<#include "head.ftl"><#include "foot.ftl">
head.ftl

文件位置:resources/templates/body/head.ft

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:v="urn:schemas-microsoft-com:vml"xmlns:w10="urn:schemas-microsoft-com:office:word"xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core"xmlns:aml="http://schemas.microsoft.com/aml/2001/core"xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no" xml:space="preserve"xmlns:wpsCustomData="http://www.wps.cn/officeDocument/2013/wpsCustomData"><o:DocumentProperties><o:Author>AAA</o:Author><o:LastAuthor>下雨云多</o:LastAuthor><o:Created>2021-11-24T08:03:35Z</o:Created><o:LastSaved>2021-11-25T03:16:02Z</o:LastSaved><o:TotalTime>1440</o:TotalTime><o:Pages>1</o:Pages><o:Words>0</o:Words><o:Characters>0</o:Characters><o:Lines>0</o:Lines><o:Paragraphs>0</o:Paragraphs><o:CharactersWithSpaces>0</o:CharactersWithSpaces><o:Version>14</o:Version></o:DocumentProperties><o:CustomDocumentProperties><o:KSOProductBuildVer dt:dt="string">2052-11.1.0.11115</o:KSOProductBuildVer><o:ICV dt:dt="string">E3221C35A2854E0594E49D338A22A41F</o:ICV></o:CustomDocumentProperties><w:fonts><w:defaultFonts w:ascii="Calibri" w:fareast="宋体" w:h-ansi="Calibri" w:cs="Times New Roman"/><w:font w:name="Times New Roman"><w:panose-1 w:val="02020603050405020304"/><w:charset w:val="00"/><w:family w:val="Auto"/><w:pitch w:val="Default"/><w:sig w:usb-0="E0002EFF" w:usb-1="C000785B" w:usb-2="00000009" w:usb-3="00000000" w:csb-0="400001FF"w:csb-1="FFFF0000"/></w:font><w:font w:name="宋体"><w:panose-1 w:val="02010600030101010101"/><w:charset w:val="86"/><w:family w:val="Auto"/><w:pitch w:val="Default"/><w:sig w:usb-0="00000003" w:usb-1="288F0000" w:usb-2="00000006" w:usb-3="00000000" w:csb-0="00040001"w:csb-1="00000000"/></w:font><w:font w:name="Wingdings"><w:panose-1 w:val="05000000000000000000"/><w:charset w:val="02"/><w:family w:val="Auto"/><w:pitch w:val="Default"/><w:sig w:usb-0="00000000" w:usb-1="00000000" w:usb-2="00000000" w:usb-3="00000000" w:csb-0="80000000"w:csb-1="00000000"/></w:font><w:font w:name="Arial"><w:panose-1 w:val="020B0604020202020204"/><w:charset w:val="01"/><w:family w:val="SWiss"/><w:pitch w:val="Default"/><w:sig w:usb-0="E0002EFF" w:usb-1="C000785B" w:usb-2="00000009" w:usb-3="00000000" w:csb-0="400001FF"w:csb-1="FFFF0000"/></w:font><w:font w:name="黑体"><w:panose-1 w:val="02010609060101010101"/><w:charset w:val="86"/><w:family w:val="Auto"/><w:pitch w:val="Default"/><w:sig w:usb-0="800002BF" w:usb-1="38CF7CFA" w:usb-2="00000016" w:usb-3="00000000" w:csb-0="00040001"w:csb-1="00000000"/></w:font><w:font w:name="Courier New"><w:panose-1 w:val="02070309020205020404"/><w:charset w:val="01"/><w:family w:val="Modern"/><w:pitch w:val="Default"/><w:sig w:usb-0="E0002EFF" w:usb-1="C0007843" w:usb-2="00000009" w:usb-3="00000000" w:csb-0="400001FF"w:csb-1="FFFF0000"/></w:font><w:font w:name="Symbol"><w:panose-1 w:val="05050102010706020507"/><w:charset w:val="02"/><w:family w:val="Roman"/><w:pitch w:val="Default"/><w:sig w:usb-0="00000000" w:usb-1="00000000" w:usb-2="00000000" w:usb-3="00000000" w:csb-0="80000000"w:csb-1="00000000"/></w:font><w:font w:name="Calibri"><w:panose-1 w:val="020F0502020204030204"/><w:charset w:val="00"/><w:family w:val="SWiss"/><w:pitch w:val="Default"/><w:sig w:usb-0="E4002EFF" w:usb-1="C000247B" w:usb-2="00000009" w:usb-3="00000000" w:csb-0="200001FF"w:csb-1="00000000"/></w:font><w:font w:name="Verdana"><w:panose-1 w:val="020B0604030504040204"/><w:charset w:val="00"/><w:family w:val="Auto"/><w:pitch w:val="Default"/><w:sig w:usb-0="A00006FF" w:usb-1="4000205B" w:usb-2="00000010" w:usb-3="00000000" w:csb-0="2000019F"w:csb-1="00000000"/></w:font><w:font w:name="微软雅黑"><w:panose-1 w:val="020B0503020204020204"/><w:charset w:val="86"/><w:family w:val="Auto"/><w:pitch w:val="Default"/><w:sig w:usb-0="80000287" w:usb-1="2ACF3C50" w:usb-2="00000016" w:usb-3="00000000" w:csb-0="0004001F"w:csb-1="00000000"/></w:font></w:fonts><w:styles><w:latentStyles w:defLockedState="off" w:latentStyleCount="260"><w:lsdException w:name="Normal"/><w:lsdException w:name="heading 1"/><w:lsdException w:name="heading 2"/><w:lsdException w:name="heading 3"/><w:lsdException w:name="heading 4"/><w:lsdException w:name="heading 5"/><w:lsdException w:name="heading 6"/><w:lsdException w:name="heading 7"/><w:lsdException w:name="heading 8"/><w:lsdException w:name="heading 9"/><w:lsdException w:name="index 1"/><w:lsdException w:name="index 2"/><w:lsdException w:name="index 3"/><w:lsdException w:name="index 4"/><w:lsdException w:name="index 5"/><w:lsdException w:name="index 6"/><w:lsdException w:name="index 7"/><w:lsdException w:name="index 8"/><w:lsdException w:name="index 9"/><w:lsdException w:name="toc 1"/><w:lsdException w:name="toc 2"/><w:lsdException w:name="toc 3"/><w:lsdException w:name="toc 4"/><w:lsdException w:name="toc 5"/><w:lsdException w:name="toc 6"/><w:lsdException w:name="toc 7"/><w:lsdException w:name="toc 8"/><w:lsdException w:name="toc 9"/><w:lsdException w:name="Normal Indent"/><w:lsdException w:name="footnote text"/><w:lsdException w:name="annotation text"/><w:lsdException w:name="header"/><w:lsdException w:name="footer"/><w:lsdException w:name="index heading"/><w:lsdException w:name="caption"/><w:lsdException w:name="table of figures"/><w:lsdException w:name="envelope address"/><w:lsdException w:name="envelope return"/><w:lsdException w:name="footnote reference"/><w:lsdException w:name="annotation reference"/><w:lsdException w:name="line number"/><w:lsdException w:name="page number"/><w:lsdException w:name="endnote reference"/><w:lsdException w:name="endnote text"/><w:lsdException w:name="table of authorities"/><w:lsdException w:name="macro"/><w:lsdException w:name="toa heading"/><w:lsdException w:name="List"/><w:lsdException w:name="List Bullet"/><w:lsdException w:name="List Number"/><w:lsdException w:name="List 2"/><w:lsdException w:name="List 3"/><w:lsdException w:name="List 4"/><w:lsdException w:name="List 5"/><w:lsdException w:name="List Bullet 2"/><w:lsdException w:name="List Bullet 3"/><w:lsdException w:name="List Bullet 4"/><w:lsdException w:name="List Bullet 5"/><w:lsdException w:name="List Number 2"/><w:lsdException w:name="List Number 3"/><w:lsdException w:name="List Number 4"/><w:lsdException w:name="List Number 5"/><w:lsdException w:name="Title"/><w:lsdException w:name="Closing"/><w:lsdException w:name="Signature"/><w:lsdException w:name="Default Paragraph Font"/><w:lsdException w:name="Body Text"/><w:lsdException w:name="Body Text Indent"/><w:lsdException w:name="List Continue"/><w:lsdException w:name="List Continue 2"/><w:lsdException w:name="List Continue 3"/><w:lsdException w:name="List Continue 4"/><w:lsdException w:name="List Continue 5"/><w:lsdException w:name="Message Header"/><w:lsdException w:name="Subtitle"/><w:lsdException w:name="Salutation"/><w:lsdException w:name="Date"/><w:lsdException w:name="Body Text First Indent"/><w:lsdException w:name="Body Text First Indent 2"/><w:lsdException w:name="Note Heading"/><w:lsdException w:name="Body Text 2"/><w:lsdException w:name="Body Text 3"/><w:lsdException w:name="Body Text Indent 2"/><w:lsdException w:name="Body Text Indent 3"/><w:lsdException w:name="Block Text"/><w:lsdException w:name="Hyperlink"/><w:lsdException w:name="FollowedHyperlink"/><w:lsdException w:name="Strong"/><w:lsdException w:name="Emphasis"/><w:lsdException w:name="Document Map"/><w:lsdException w:name="Plain Text"/><w:lsdException w:name="E-mail Signature"/><w:lsdException w:name="Normal (Web)"/><w:lsdException w:name="HTML Acronym"/><w:lsdException w:name="HTML Address"/><w:lsdException w:name="HTML Cite"/><w:lsdException w:name="HTML Code"/><w:lsdException w:name="HTML Definition"/><w:lsdException w:name="HTML Keyboard"/><w:lsdException w:name="HTML Preformatted"/><w:lsdException w:name="HTML Sample"/><w:lsdException w:name="HTML Typewriter"/><w:lsdException w:name="HTML Variable"/><w:lsdException w:name="Normal Table"/><w:lsdException w:name="annotation subject"/><w:lsdException w:name="Table Simple 1"/><w:lsdException w:name="Table Simple 2"/><w:lsdException w:name="Table Simple 3"/><w:lsdException w:name="Table Classic 1"/><w:lsdException w:name="Table Classic 2"/><w:lsdException w:name="Table Classic 3"/><w:lsdException w:name="Table Classic 4"/><w:lsdException w:name="Table Colorful 1"/><w:lsdException w:name="Table Colorful 2"/><w:lsdException w:name="Table Colorful 3"/><w:lsdException w:name="Table Columns 1"/><w:lsdException w:name="Table Columns 2"/><w:lsdException w:name="Table Columns 3"/><w:lsdException w:name="Table Columns 4"/><w:lsdException w:name="Table Columns 5"/><w:lsdException w:name="Table Grid 1"/><w:lsdException w:name="Table Grid 2"/><w:lsdException w:name="Table Grid 3"/><w:lsdException w:name="Table Grid 4"/><w:lsdException w:name="Table Grid 5"/><w:lsdException w:name="Table Grid 6"/><w:lsdException w:name="Table Grid 7"/><w:lsdException w:name="Table Grid 8"/><w:lsdException w:name="Table List 1"/><w:lsdException w:name="Table List 2"/><w:lsdException w:name="Table List 3"/><w:lsdException w:name="Table List 4"/><w:lsdException w:name="Table List 5"/><w:lsdException w:name="Table List 6"/><w:lsdException w:name="Table List 7"/><w:lsdException w:name="Table List 8"/><w:lsdException w:name="Table 3D effects 1"/><w:lsdException w:name="Table 3D effects 2"/><w:lsdException w:name="Table 3D effects 3"/><w:lsdException w:name="Table Contemporary"/><w:lsdException w:name="Table Elegant"/><w:lsdException w:name="Table Professional"/><w:lsdException w:name="Table Subtle 1"/><w:lsdException w:name="Table Subtle 2"/><w:lsdException w:name="Table Web 1"/><w:lsdException w:name="Table Web 2"/><w:lsdException w:name="Table Web 3"/><w:lsdException w:name="Balloon Text"/><w:lsdException w:name="Table Grid"/><w:lsdException w:name="Table Theme"/><w:lsdException w:name="Light Shading"/><w:lsdException w:name="Light List"/><w:lsdException w:name="Light Grid"/><w:lsdException w:name="Medium Shading 1"/><w:lsdException w:name="Medium Shading 2"/><w:lsdException w:name="Medium List 1"/><w:lsdException w:name="Medium List 2"/><w:lsdException w:name="Medium Grid 1"/><w:lsdException w:name="Medium Grid 2"/><w:lsdException w:name="Medium Grid 3"/><w:lsdException w:name="Dark List"/><w:lsdException w:name="Colorful Shading"/><w:lsdException w:name="Colorful List"/><w:lsdException w:name="Colorful Grid"/><w:lsdException w:name="Light Shading Accent 1"/><w:lsdException w:name="Light List Accent 1"/><w:lsdException w:name="Light Grid Accent 1"/><w:lsdException w:name="Medium Shading 1 Accent 1"/><w:lsdException w:name="Medium Shading 2 Accent 1"/><w:lsdException w:name="Medium List 1 Accent 1"/><w:lsdException w:name="Medium List 2 Accent 1"/><w:lsdException w:name="Medium Grid 1 Accent 1"/><w:lsdException w:name="Medium Grid 2 Accent 1"/><w:lsdException w:name="Medium Grid 3 Accent 1"/><w:lsdException w:name="Dark List Accent 1"/><w:lsdException w:name="Colorful Shading Accent 1"/><w:lsdException w:name="Colorful List Accent 1"/><w:lsdException w:name="Colorful Grid Accent 1"/><w:lsdException w:name="Light Shading Accent 2"/><w:lsdException w:name="Light List Accent 2"/><w:lsdException w:name="Light Grid Accent 2"/><w:lsdException w:name="Medium Shading 1 Accent 2"/><w:lsdException w:name="Medium Shading 2 Accent 2"/><w:lsdException w:name="Medium List 1 Accent 2"/><w:lsdException w:name="Medium List 2 Accent 2"/><w:lsdException w:name="Medium Grid 1 Accent 2"/><w:lsdException w:name="Medium Grid 2 Accent 2"/><w:lsdException w:name="Medium Grid 3 Accent 2"/><w:lsdException w:name="Dark List Accent 2"/><w:lsdException w:name="Colorful Shading Accent 2"/><w:lsdException w:name="Colorful List Accent 2"/><w:lsdException w:name="Colorful Grid Accent 2"/><w:lsdException w:name="Light Shading Accent 3"/><w:lsdException w:name="Light List Accent 3"/><w:lsdException w:name="Light Grid Accent 3"/><w:lsdException w:name="Medium Shading 1 Accent 3"/><w:lsdException w:name="Medium Shading 2 Accent 3"/><w:lsdException w:name="Medium List 1 Accent 3"/><w:lsdException w:name="Medium List 2 Accent 3"/><w:lsdException w:name="Medium Grid 1 Accent 3"/><w:lsdException w:name="Medium Grid 2 Accent 3"/><w:lsdException w:name="Medium Grid 3 Accent 3"/><w:lsdException w:name="Dark List Accent 3"/><w:lsdException w:name="Colorful Shading Accent 3"/><w:lsdException w:name="Colorful List Accent 3"/><w:lsdException w:name="Colorful Grid Accent 3"/><w:lsdException w:name="Light Shading Accent 4"/><w:lsdException w:name="Light List Accent 4"/><w:lsdException w:name="Light Grid Accent 4"/><w:lsdException w:name="Medium Shading 1 Accent 4"/><w:lsdException w:name="Medium Shading 2 Accent 4"/><w:lsdException w:name="Medium List 1 Accent 4"/><w:lsdException w:name="Medium List 2 Accent 4"/><w:lsdException w:name="Medium Grid 1 Accent 4"/><w:lsdException w:name="Medium Grid 2 Accent 4"/><w:lsdException w:name="Medium Grid 3 Accent 4"/><w:lsdException w:name="Dark List Accent 4"/><w:lsdException w:name="Colorful Shading Accent 4"/><w:lsdException w:name="Colorful List Accent 4"/><w:lsdException w:name="Colorful Grid Accent 4"/><w:lsdException w:name="Light Shading Accent 5"/><w:lsdException w:name="Light List Accent 5"/><w:lsdException w:name="Light Grid Accent 5"/><w:lsdException w:name="Medium Shading 1 Accent 5"/><w:lsdException w:name="Medium Shading 2 Accent 5"/><w:lsdException w:name="Medium List 1 Accent 5"/><w:lsdException w:name="Medium List 2 Accent 5"/><w:lsdException w:name="Medium Grid 1 Accent 5"/><w:lsdException w:name="Medium Grid 2 Accent 5"/><w:lsdException w:name="Medium Grid 3 Accent 5"/><w:lsdException w:name="Dark List Accent 5"/><w:lsdException w:name="Colorful Shading Accent 5"/><w:lsdException w:name="Colorful List Accent 5"/><w:lsdException w:name="Colorful Grid Accent 5"/><w:lsdException w:name="Light Shading Accent 6"/><w:lsdException w:name="Light List Accent 6"/><w:lsdException w:name="Light Grid Accent 6"/><w:lsdException w:name="Medium Shading 1 Accent 6"/><w:lsdException w:name="Medium Shading 2 Accent 6"/><w:lsdException w:name="Medium List 1 Accent 6"/><w:lsdException w:name="Medium List 2 Accent 6"/><w:lsdException w:name="Medium Grid 1 Accent 6"/><w:lsdException w:name="Medium Grid 2 Accent 6"/><w:lsdException w:name="Medium Grid 3 Accent 6"/><w:lsdException w:name="Dark List Accent 6"/><w:lsdException w:name="Colorful Shading Accent 6"/><w:lsdException w:name="Colorful List Accent 6"/><w:lsdException w:name="Colorful Grid Accent 6"/></w:latentStyles><w:style w:type="paragraph" w:styleId="a1" w:default="on"><w:name w:val="Normal"/><w:pPr><w:widowControl w:val="off"/><w:jc w:val="both"/></w:pPr><w:rPr><w:rFonts w:ascii="Times New Roman" w:h-ansi="Times New Roman" w:fareast="宋体" w:cs="Times New Roman"w:hint="default"/><w:kern w:val="2"/><w:sz w:val="21"/><w:sz-cs w:val="24"/><w:lang w:val="EN-US" w:fareast="ZH-CN" w:bidi="AR-SA"/></w:rPr></w:style><w:style w:type="character" w:styleId="a5" w:default="on"><w:name w:val="Default Paragraph Font"/><w:semiHidden/></w:style><w:style w:type="table" w:styleId="a3" w:default="on"><w:name w:val="Normal Table"/><w:semiHidden/><w:tblPr><w:tblCellMar><w:top w:w="0" w:type="dxa"/><w:left w:w="108" w:type="dxa"/><w:bottom w:w="0" w:type="dxa"/><w:right w:w="108" w:type="dxa"/></w:tblCellMar></w:tblPr></w:style><w:style w:type="paragraph" w:styleId="a2"><w:name w:val="Plain Text"/><w:basedOn w:val="a1"/><w:pPr><w:spacing w:line="360" w:line-rule="auto"/></w:pPr><w:rPr><w:rFonts w:ascii="宋体" w:h-ansi="Courier New" w:cs="Courier New" w:hint="default"/><w:sz-cs w:val="21"/></w:rPr></w:style><w:style w:type="table" w:styleId="a4"><w:name w:val="Table Grid"/><w:basedOn w:val="a3"/><w:pPr><w:pStyle w:val="a3"/><w:widowControl w:val="off"/><w:jc w:val="both"/></w:pPr><w:tblPr><w:tblBorders><w:top w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/><w:left w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/><w:bottom w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/><w:right w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/><w:insideH w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/><w:insideV w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/></w:tblBorders></w:tblPr></w:style></w:styles><w:bgPict><w:background/><v:background id="_x0000_s1025"><v:fill on="f" focussize="0,0"/></v:background></w:bgPict><w:docPr><w:view w:val="print"/><w:zoom w:percent="90"/><w:characterSpacingControl w:val="CompressPunctuation"/><w:documentProtection w:enforcement="off"/><w:punctuationKerning/><w:doNotEmbedSystemFonts/><w:bordersDontSurroundHeader/><w:bordersDontSurroundFooter/><w:defaultTabStop w:val="420"/><w:drawingGridVerticalSpacing w:val="156"/><w:displayHorizontalDrawingGridEvery w:val="0"/><w:displayVerticalDrawingGridEvery w:val="2"/><w:compat><w:adjustLineHeightInTable/><w:ulTrailSpace/><w:doNotExpandShiftReturn/><w:balanceSingleByteDoubleByteWidth/><w:useFELayout/><w:spaceForUL/><w:wrapTextWithPunct/><w:breakWrappedTables/><w:useAsianBreakRules/><w:dontGrowAutofit/><w:useFELayout/></w:compat></w:docPr><w:body><wx:sect><#--############################################################--><#--#######    医院名称 + 医院logo + 量表名称  #####################--><#--############################################################--><w:p><w:pPr><w:spacing w:line="276" w:line-rule="auto"/><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="黑体" w:fareast="黑体" w:hint="fareast"/><w:b/><w:sz w:val="30"/><w:sz-cs w:val="30"/><w:lang w:val="EN-US" w:fareast="ZH-CN"/></w:rPr></w:pPr><w:r><w:pict><w:binData w:name="wordml://1.png">${hospital_logo}</w:binData><v:shape id="_x0000_s1026" o:spt="75" alt="asia55151201106191330191" type="#_x0000_t75"style="position:absolute;left:0pt;margin-left:-3.5pt;margin-top:11.5pt;height:56.5pt;width:56.5pt;mso-position-horizontal-relative:margin;z-index:251660288;mso-width-relative:page;mso-height-relative:page;"filled="f" o:preferrelative="t" stroked="f" coordsize="21600,21600"><v:path/><v:fill on="f" focussize="0,0"/><v:stroke on="f"/><v:imagedata src="wordml://1.png" o:title="asia55151201106191330191"/><o:lock v:ext="edit" aspectratio="t"/></v:shape></w:pict></w:r><w:r><w:rPr><w:rFonts w:ascii="黑体" w:fareast="黑体" w:hint="fareast"/><w:b/><w:sz w:val="30"/><w:sz-cs w:val="30"/></w:rPr><w:t>${hospital_name}</w:t></w:r></w:p><#--医院名称 + 医院logo--><w:p><w:pPr><w:spacing w:line="276" w:line-rule="auto"/><w:jc w:val="center"/><w:rPr><w:rFonts w:fareast="宋体" w:hint="fareast"/><w:b/><w:sz w:val="28"/><w:sz-cs w:val="28"/><w:lang w:val="EN-US" w:fareast="ZH-CN"/></w:rPr></w:pPr><w:r><w:rPr><w:b/><w:sz w:val="28"/><w:sz-cs w:val="28"/></w:rPr><w:t>${scale_name}</w:t></w:r></w:p><#--量表名称--><w:p><w:pPr><w:jc w:val="center"/></w:pPr></w:p>
foot.ftl

文件位置:resources/templates/body/foot.ftl

            <w:sectPr><w:pgSz w:w="11906" w:h="16838"/><w:pgMar w:top="1417" w:right="1417" w:bottom="1417" w:left="1417" w:header="851" w:footer="992"w:gutter="0"/><w:cols w:space="720"/><w:docGrid w:type="lines" w:line-pitch="312"/></w:sectPr></wx:sect></w:body>
</w:wordDocument>
http请求
POST http://localhost:8080/word

请看源代码(引用json文件)

生成word文档,需要使用到模板目录

导入依赖,一个启动类JarApplication

 <!--json包-->
<dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.1</version><classifier>jdk15</classifier>
</dependency><!--io文件上传-->
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.6</version>
</dependency>
启动类

打成jar包后直接运行,不需要输入网址,直接测试

package com.jar;import com.jar.demo.JarPathDemo;
import net.sf.json.JSONArray;
import org.apache.commons.io.FileUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;import java.io.*;
import java.nio.charset.Charset;@SpringBootApplication
public class JarApplication {public static void main(String[] args) throws IOException {SpringApplication.run(JarApplication.class, args);String path = "static/js/json/16.json";//该目录是以resources目录为根目录//文件内容直接转为String类型InputStream is = jarPathDemo.getClass().getClassLoader().getResourceAsStream(path);BufferedReader in = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));StringBuffer buf = new StringBuffer();String line = "";while ((line = in.readLine()) != null){buf.append(line);}String input = buf.toString();jsonArray = JSONArray.fromObject((Object)input);System.out.println(jsonArray);}}

spring-boot引用资源:图片、json文件、模板目录(jar包运行依旧有效)相关推荐

  1. Spring Boot 静态资源映射与上传文件路由配置

    默认静态资源映射目录 默认映射路径 在平常的 web 开发中,避免不了需要访问静态资源,如常规的样式,JS,图片,上传文件等;Spring Boot 默认配置对静态资源映射提供了如下路径的映射 /st ...

  2. spring boot读取resources下面的文件图片

    spring boot读取resources下面的文件图片 下面的代码是为了保证在打成jar包的情况下依然能够有效读取到文件. 先看项目目录结构: 我是想读取resources下面的图片,下面放上代码 ...

  3. Spring Boot Freemark HTML 生成 PDF、生成水印Logo、docx文件生成PDF,Jar包运行可读取模板文件、字体文件

    用于通过模板生成PDF,在项目中生成个人授权协议函.个人电子保单.流水报表,数据报表等,将HTML静态模板写出来后,将数据替换成动态数据即可. <!-- html2pdf --> < ...

  4. Spring Boot 推荐的基础 POM 文件

    Spring Boot 推荐的基础 POM 文件 名称 说明 spring-boot-starter 核心 POM,包含自动配置支持.日志库和对 YAML 配置文件的支持. spring-boot-s ...

  5. [maven][spring boot] mvn -f 指定pom文件

    前言 spring boot 2.0 + mvn 3.5 项目 测试环境中运行spring boot时,需要指定属性文件为:application-test 使用 方式时,指定属性文件未起效 经过尝试 ...

  6. java配置文件放置到jar外_java相关:Spring Boot 把配置文件和日志文件放到jar外部...

    java相关:Spring Boot 把配置文件和日志文件放到jar外部 发布于 2020-3-6| 复制链接 如果不想使用默认的application.properties,而想将属性文件放到jar ...

  7. spring boot结合FastDFSClient做下载文件注意事项

    spring boot结合FastDFSClient做下载文件注意事项 1.后台下载方法走完后,前端页面浏览器一直没出现下载框. 2.ie浏览器兼容问题. 下面的FastDFSClient类依赖fdf ...

  8. java jar包资源文件_深入jar包:从jar包中读取资源文件

    我们常常在代码中读取一些资源文件(比如图片,音乐,文本等等).在单独运行的时候这些简单的处理当然不会有问题.但是,如果我们把代码打成一个jar包以后,即使将资源文件一并打包,这些东西也找不出来了.看看 ...

  9. Spring Boot 核心原理与源码解析 - 目录

    准备重新写 SpringBoot 配置文件解析原理 , 先在这里把要写的内容记下来 Spring Boot 核心原理与源码解析 - 目录 1\何时解析\如何解析 application.propert ...

最新文章

  1. 分布式环境下,互斥性与幂等性问题,分析与解决思路
  2. 我的理解:什么是WCF
  3. linux编译gcc5.1.0,linux编译gcc-5.1.0
  4. 【转】删除数据库中重复数据的几个方法
  5. 当债务危机遇上限贷放松 房价投资价值大不如前
  6. 【MTSP】基于matlab蚁群和粒子群算法求解多旅行商问题【含Matlab code 1156期】
  7. Mugeda(木疙瘩)H5案例课—重力感应类H5-岑远科-专题视频课程
  8. matlab Fsw,个人主页
  9. 京东后台:订单履约系统设计(下)
  10. 【Windows】Windows10 企业版 LTSC/Windows Server 2019如何安装应用商店和UWP应用?
  11. (Attention机制原文)论文阅读:Neural Machine Translation by Jointly Learning to Align and Translate
  12. ctfhsow web入门 命令执行大全
  13. Android添加手机黑名单,手机来电拦截实现详解与Demo,一个不错的练手项目,涵盖Android四大组件。
  14. uni-app—使用vscode创建小程序项目(图文)
  15. 赵小楼《天道》《遥远的救世主》深度解析(81)一个不愿躺着对话的女人,应给与绝对尊严,这符合强势文化范畴
  16. Verilog实现一个8位带进位全加器
  17. excel 累加每个sheet中的值
  18. 互相关函数以及Matlab仿真
  19. post提交php出现502,PHP+Nginx 字符串内容 POST 提交502错误?
  20. amazon白皮书学习 I

热门文章

  1. SQL中union的常用用法~~解决数据拼凑、表中两列合并等问题
  2. Go | 一分钟掌握Go | 1 - 安装Go
  3. 江苏大学和南邮的计算机专业,江苏这所大学很憋屈,与南京大学同出一脉,认可度高却不是211...
  4. html5播放器源码自己添加音乐,HTML5 audio标签 打造属于自己的音乐播放器
  5. why inverse must be setted in hibernate bidirectional association of one-to-many and many-to-many
  6. unraid上实现阿里云盘与nas同步
  7. ERP和SAP之间的关系
  8. php新手学习php的好网站(推荐!!)
  9. 《BangDream》:从乐队企划到音乐游戏
  10. 5min检测Fc融合蛋白