springboot下载doc⽂档
1、先个⼀个doc的⽂件,另存为⼀份xml格式的⽂件,把名字改成fit⽂件放到resources⽬录结构下⾯
2、修改pom⽂件,不然打包⽆法将resources⽬录下⾯的ftl⽂件打包进去
3、配置⼀个javaBean
import t.annotation.Bean;
import t.annotation.Configuration;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import java.util.HashMap;
import java.util.Map;
/**
* @Auther: lyl
* @Date: 2020/12/5 10:41
* @Description:
*/
@Configuration
public class ApplicationConfig {
@Bean(name = "freeMarkerConfigurer")
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setDefaultEncoding("UTF-8");
configurer.setTemplateLoaderPath("classpath:/templates");
Map<String, Object> variables = new HashMap<>(1<<1);
variables.put("xml_escape","fmXmlEscape");
configurer.setFreemarkerVariables(variables);
return configurer;
}
}
4、注⼊这个bean,然后获取模板⽂件
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
public void findByIdSelectDownload(Long id, HttpServletResponse response) {
CaseDTO byIdSelect = this.findByIdSelect(id);
Map<String, Object> dataMap = new HashMap<>();
// 创建configuration对象,得到模板⽂件保存的⽬录
Configuration configuration = Configuration();
/
/ 加载⼀个模板⽂件,创建⼀个模板对象
Locale locale = new Locale("zh");
Template t=null;
try {
t = Template("case.ftl",locale,"UTF-8"); //获取模板⽂件
} catch (IOException e) {
e.printStackTrace();
}
Writer out = null;
springboot结构
try {
response.setContentType("multipart/form-data;charset=utf-8");
response.setHeader("Content-disposition", "attachment; filename=" + ("caseId")+".doc");
response.addHeader("Content-type", "application/octet-stream");
out = Writer();
t.process(dataMap, out); //将填充数据填⼊模板⽂件并输出到⽬标⽂件
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(out != null) {
out.close();
}
//                if(outFile != null) {
//                    outFile.delete();
//                }
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意:这边循环下载图⽚遇到的bug
<#list images as im>
<w:pict>
<v:shapetype id="_x0000_t75"coordsize="21600,21600"o:spt="75"o:preferrelative="t"path="m@4@5l@4@11@9@11@9@5xe"filled="f"stroked="f"> <v:stroke join/>
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0"/>
<v:f eqn="sum @0 1 0"/>
<v:f eqn="sum 0 0 @1"/>
<v:f eqn="prod @2 1 2"/>
<v:f eqn="prod @3 21600 pixelWidth"/>
<v:f eqn="prod @3 21600 pixelHeight"/>
<v:f eqn="sum @0 0 1"/>
<v:f eqn="prod @6 1 2"/>
<v:f eqn="prod @7 21600 pixelWidth"/>
<v:f eqn="sum @8 21600 0"/>
<v:f eqn="prod @7 21600 pixelHeight"/>
<v:f eqn="sum @10 21600 0"/>
</v:formulas>
<v:path o:extrusionok="f"gradientshapeok="t"o:connecttype="rect"/>
<o:lock v:ext="edit"aspectratio="t"/>
</v:shapetype>
<w:binData w:name="${"wordml://0200000"+im_index+1+".jpg"}"xml:space="preserve">${im}</w:binData>
<v:shape id="图⽚"o:spid="_x0000_i1025"type="#_x0000_t75">
<v:imagedata src="${"wordml://0200000"+im_index+1+".jpg"}" o:title="菜单"/>
</v:shape>
</w:pict>
</#list>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。