• java下载文件成word


    1.pom中指定资源路径和不让maven压缩docx文件

    指定资源路径如下:

    **/*.docx
    
    • 1

    不压缩docx文件如下:

    
    	org.apache.maven.plugins
    	maven-resources-plugin
    	
          
                docx
                
           
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
     
                simple
                
                    
                        src/main/java
                        
                            **/*.yml
                            **/*.properties
                            **/*.xml
    
                        
                        false
                    
                    
                        src/main/resources
                        
                            **/*.yml
                            **/*.properties
                            **/*.xml
    
                            **/*.docx
                        
                        true
                    
                
    
    
                
    
                    
                        org.springframework.boot
                        spring-boot-maven-plugin
                    
    
    
                    
                    
                        org.apache.maven.plugins
                        maven-resources-plugin
                        
                            
                                docx
                            
                        
                    
    
                
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    坑:

    1.不设置资源路径爆出找不到文件异常:
    java.io.FileNotFoundException: class path resource [*/] cannot be opened because it does not exist
    2.不设置 docx会将docx文件加压,报出加压异常:
    java.util.zip.ZipException: Unexpected record signature: 0X9

    2.pom添加poi依赖

     			
                
                    com.deepoove
                    poi-tl
                    1.12.0
                    
                        
                            
                            org.slf4j
                            slf4j-api
                        
                    
                
                
                    org.apache.poi
                    poi
                    5.2.2
                
                
                    org.apache.poi
                    poi-ooxml
                    5.2.2
                
                
                    org.apache.poi
                    poi-ooxml-schemas
                    4.1.2
                
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    3.service接口

    public interface CherryService extends IService {
        void export(HttpServletResponse response) throws IOException;
    }
    
    • 1
    • 2
    • 3

    4.CherryServiceImpl

    package com.cherry.manager.service.impl;
    
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.cherry.manager.entity.Cherry;
    import com.cherry.manager.dao.CherryDao;
    import com.cherry.manager.service.CherryService;
    import com.deepoove.poi.XWPFTemplate;
    import com.deepoove.poi.util.PoitlIOUtils;
    import com.google.common.collect.Maps;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.stereotype.Service;
    import javax.servlet.http.HttpServletResponse;
    import java.io.*;
    import java.net.URLEncoder;
    import java.util.Map;
    
    /**
     * (Cherry)表服务实现类
     *
     * @since 2022-09-21 11:12:53
     */
    @Service
    public class CherryServiceImpl extends ServiceImpl implements CherryService {
    
    
        /**
         * 生成word文档
         * @param response
         */
        @Override
        public void export(HttpServletResponse response) throws IOException {
           // 假设要输出的内容,实际从数据库取
           Cherry cherry = new Cherry();
           cherry.setId("01");
           cherry.setName("车厘子");
    
            // 文件名称
            String fileName = System.currentTimeMillis()+"车厘子"+".docx";
    
            // word文档里设置内容
            Map dataMap = Maps.newHashMap();
            dataMap.put("id",cherry.getId());
            dataMap.put("name",cherry.getName());
    
            // 获得资源路径,渲染数据
            XWPFTemplate template = XWPFTemplate.compile(new ClassPathResource("doc/cherry.docx").getInputStream()).render(dataMap);
    
            // 设置让浏览器下载文件
            response.setContentType("application/force-download");
    
            // 设置输出文件名
            response.setHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode(fileName,"UTF-8"));
    
            // 输出流
            OutputStream out = response.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(out);
            template.write(bos);
            bos.flush();
            out.flush();
    
            // 关闭所有流
            PoitlIOUtils.closeQuietlyMulti(template, bos, out);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
  • 相关阅读:
    SpringMVC篇
    基于springboot thymeleaf的电商项目
    AopContext对象原理
    Kotlin 开发Android app(十一):Android控件RecyclerView
    mybatis总结
    【企业级SpringBoot单体项目模板 】—— 一些开发规范
    工作中遇到的问题与解决办法(三)
    设计模式学习笔记 - 规范与重构 - 5.如何通过封装、抽象、模块化、中间层解耦代码?
    概率有向图模型(一)
    ​力扣解法汇总1775. 通过最少操作次数使数组的和相等
  • 原文地址:https://blog.csdn.net/m0_49382941/article/details/127429434