springboot下载doc文档


1、先找个一个doc的文件,另存为一份xml格式的文件,把名字改成fit文件放到resources目录结构下面

 

 

 

 2、修改pom文件,不然打包无法将resources目录下面的ftl文件打包进去

 

 3、配置一个javaBean

import org.springframework.context.annotation.Bean;
import org.springframework.context.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<>();
        this.getData(dataMap,byIdSelect);
        // 创建configuration对象,得到模板文件保存的目录
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        // 加载一个模板文件,创建一个模板对象
        Locale locale = new Locale("zh");
        Template t=null;
        try {
            t = configuration.getTemplate("case.ftl",locale,"UTF-8"); //获取模板文件
        } catch (IOException e) {
            e.printStackTrace();
        }
        Writer out = null;
        try {
            response.setContentType("multipart/form-data;charset=utf-8");
            response.setHeader("Content-disposition", "attachment; filename=" + dataMap.get("caseId")+".doc");
            response.addHeader("Content-type", "application/octet-stream");
            out = response.getWriter();
            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   joinstyle  =  "miter"  />
         <  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"   style  =  "width:414.75pt;height:207.75pt;visibility:visible;mso-wrap-style:square"  >
         < v:imagedata  src="${"  wordml://0200000"+im_index+1+".jpg"}  o:title  =  "菜单"  />
        </  v:shape  >
       </  w:pict  >
       </#list>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM