jiazx0107
2025-11-18 b0bf6e809ec4990ca8e3de95456c90860e49e2a5
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.fzzy.eoms.file.controller;
 
import com.fzzy.eoms.file.CoreFileService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Date;
 
/**
 * @program: eoms
 * @description: 文件操作
 * @author: 0range
 * @create: 2021-01-05 17:05
 **/
@Slf4j
@RestController
@RequestMapping("/file")
public class FileController {
 
 
    @Resource
    private CoreFileService fileService;
 
    /**
     * 附件下载
     * @param fileId
     * @param fileDate
     * @return
     * @throws Exception
     */
    @GetMapping("/download")
    public void download(@RequestParam(value = "id", required = true) String fileId,
                         @RequestParam(value = "date", required = true) String fileDate,
                         HttpServletResponse response) throws Exception {
 
        log.info("-----download-----fileId={},fileDate={}", fileId, fileDate);
 
        // 将字符串转换为 long
        long timestamp = Long.parseLong(fileDate);
        Date date = new Date(timestamp);
 
        String storePath = fileService.getStoragePath(date);
 
        String filePath = storePath + fileId;
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        FileUtils.setAttachmentResponseHeader(response, fileId);
        FileUtils.writeBytes(filePath, response.getOutputStream());
    }
 
    /**
     * 附件预览,仅图片和PDF类支持预览
     * @param fileId
     * @param fileDate
     * @return
     * @throws Exception
     */
    @GetMapping("/preview")
    public void preview(@RequestParam(value = "id", required = true) String fileId,
                        @RequestParam(value = "date", required = true) String fileDate,
                        HttpServletResponse response){
 
        log.info("-----preview-----fileId={},fileDate={}", fileId, fileDate);
 
        // 将字符串转换为 long
        long timestamp = Long.parseLong(fileDate);
        Date date = new Date(timestamp);
 
        String storePath = fileService.getStoragePath(date);
 
        String filePath = storePath + fileId;
 
        File file = new File(filePath);
        response.setHeader("Content-Disposition", "inline; filename*=UTF-8''" + fileId);
        ServletOutputStream os = null;
        try {
            os = response.getOutputStream();
            os.write(org.apache.commons.io.FileUtils.readFileToByteArray(file));
            os.flush();
        } catch (Exception e) {
            log.error("-----preview-----fileId={},fileDate={},error={}", fileId, fileDate, e.getMessage());
            log.error("preview error ",e);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (Exception e) {
                    log.error("-----preview-----fileId={},fileDate={},error={}", fileId, fileDate, e.getMessage());
                    log.error("preview error ",e);
                }
            }
        }
    }
 
 
 
}