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);
}
}