java下载文件成word

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

指定资源路径如下:

**/*.docx

不压缩docx文件如下:


	org.apache.maven.plugins
	maven-resources-plugin
	
      
            docx
            
       

 
            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.不设置资源路径爆出找不到文件异常:
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
            

3.service接口

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

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

}

你可能感兴趣的:(java思路,java,word,开发语言)