peter
2026-01-11 9d65dd091154a0301076549e031e15017898c2ee
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.nq.controller;
 
import com.nq.utils.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
 
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
 
@Controller
public class ImageController {
    private static final Logger log = LoggerFactory.getLogger(ImageController.class);
 
    @RequestMapping({"/imgs/{filename:.+}"})
    public void getImage(@PathVariable String filename, HttpServletResponse response) {
        FileInputStream fis = null;
        OutputStream os = null;
        try {
            String pdfDir = PropertiesUtil.getProperty("loca.pdf.dir");
            File imageFile = new File(pdfDir, filename);
 
            if (!imageFile.exists()) {
                log.warn("图片文件不存在: {}", imageFile.getAbsolutePath());
                response.sendError(HttpServletResponse.SC_NOT_FOUND, "图片文件不存在");
                return;
            }
 
            // 设置响应头
            response.setContentType("image/png");
            response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");
            response.setContentLengthLong(imageFile.length());
            response.setHeader("Cache-Control", "public, max-age=3600");
 
            fis = new FileInputStream(imageFile);
            os = response.getOutputStream();
 
            byte[] buffer = new byte[8192];
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.flush();
        } catch (Exception e) {
            log.error("读取图片文件失败: {}", filename, e);
            try {
                if (!response.isCommitted()) {
                    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "读取图片失败");
                }
            } catch (IOException ex) {
                log.error("发送错误响应失败", ex);
            }
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    log.error("关闭文件流失败", e);
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    log.error("关闭输出流失败", e);
                }
            }
        }
    }
 
    @RequestMapping({"imgs.do"})
    public void getImageByParam(@RequestParam("file") String filename, HttpServletResponse response) {
        FileInputStream fis = null;
        OutputStream os = null;
        try {
            String pdfDir = PropertiesUtil.getProperty("loca.pdf.dir");
            File imageFile = new File(pdfDir, filename);
 
            if (!imageFile.exists()) {
                log.warn("图片文件不存在: {}", imageFile.getAbsolutePath());
                response.sendError(HttpServletResponse.SC_NOT_FOUND, "图片文件不存在");
                return;
            }
 
            // 设置响应头
            response.setContentType("image/png");
            response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");
            response.setContentLengthLong(imageFile.length());
            response.setHeader("Cache-Control", "public, max-age=3600");
 
            fis = new FileInputStream(imageFile);
            os = response.getOutputStream();
 
            byte[] buffer = new byte[8192];
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.flush();
        } catch (Exception e) {
            log.error("读取图片文件失败: {}", filename, e);
            try {
                if (!response.isCommitted()) {
                    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "读取图片失败");
                }
            } catch (IOException ex) {
                log.error("发送错误响应失败", ex);
            }
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    log.error("关闭文件流失败", e);
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    log.error("关闭输出流失败", e);
                }
            }
        }
    }
}