guoshilong
2023-11-29 1d46a2bc8a1b843eb2ee84e65fdc2f61651a18a3
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
67
68
69
70
71
72
73
74
75
76
package cn.gistack.sm.sjztmd.word.controller;
 
 
 
import cn.gistack.sm.sjztmd.word.service.ISjztmdService;
import cn.gistack.sm.sjztmd.word.util.WordUtil;
import cn.gistack.sm.sjztmd.word.vo.TbWordVO;
import com.deepoove.poi.XWPFTemplate;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @PROJECT_NAME: skjcmanager
 * @DESCRIPTION:
 * @USER: aix
 * @DATE: 2023/11/23 10:08
 */
@Slf4j
@Api(tags = "填报导出")
@RestController
@RequestMapping("/sjztmd/word")
@AllArgsConstructor
public class DownTemplateController {
 
    private final ISjztmdService sjztmdService;
 
    @GetMapping("/download/{resGuid}")
    public ResponseEntity genera(HttpServletResponse response, @PathVariable String resGuid) throws Exception {
        TbWordVO tbWordVO = sjztmdService.getTbWordVO(resGuid);
 
        String fileName = resGuid + ".docx";
        byte[] data = WordUtil.compileTemplateAndRenderData(tbWordVO);
 
        // 创建响应实体并设置响应头,将输出流作为响应体返回给客户端
        InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(data));
 
        // 将临时文件添加到响应中,以便用户下载
        return ResponseEntity.ok()
            .contentType(MediaType.APPLICATION_OCTET_STREAM) // 设置响应的内容类型为二进制流(octet stream)
            .header("Content-Disposition", "attachment; filename=\"" + fileName) // 设置响应的Content-Disposition头,以便浏览器提示用户下载文件
            .body(resource); // 将InputStreamResource对象作为响应体
    }
 
    @GetMapping("/download/eightFloodInfo")
    public ResponseEntity eightFloodInfo() throws Exception{
        String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
 
        String fileName =  time+ "全省水库防汛情况.docx";
        byte[] data = WordUtil.GetEightFloodInfo();
 
        // 创建响应实体并设置响应头,将输出流作为响应体返回给客户端
        InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(data));
 
        // 将临时文件添加到响应中,以便用户下载
        return ResponseEntity.ok()
            .contentType(MediaType.APPLICATION_OCTET_STREAM) // 设置响应的内容类型为二进制流(octet stream)
            .header("Content-Disposition", "attachment; filename=\"" + fileName) // 设置响应的Content-Disposition头,以便浏览器提示用户下载文件
            .body(resource); // 将InputStreamResource对象作为响应体
    }
 
}