1
zj
2025-06-25 a0361e762fc672d844ef15e18db5971893cce2bf
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
package project.web.api;
 
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import kernel.util.ImageDispatcher;
import kernel.util.PropertiesLoaderUtils;
 
@RestController
@CrossOrigin
public class ShowImgController {
 
    public final String basePath = "/public/showimg";
    
    private static Properties properties = PropertiesLoaderUtils.loadProperties("config/system.properties");
    
    @RequestMapping(basePath+"!showImg.action")
    public void showImg(HttpServletRequest request,HttpServletResponse response) throws Exception {
        String imagePath=request.getParameter("imagePath");
        if(null==imagePath || (imagePath=imagePath.trim()).isEmpty()) return;
        responseFile(response, imagePath);
    }
    
     /**
     * 响应输出图片文件
     * @param response
     * @param imgFile
     */
    private void responseFile(HttpServletResponse response, String imagePath) {
        try(InputStream is = getDownloadFile(imagePath);
            OutputStream os = response.getOutputStream();){
            byte [] buffer = new byte[1024]; // 图片文件流缓存池
            while(is.read(buffer) != -1){
                os.write(buffer);
            }
            os.flush();
        } catch (IOException ioe){
            ioe.printStackTrace();
        }
    }
    
    public InputStream getDownloadFile(String imagePath) throws FileNotFoundException {
        BufferedInputStream bis = null;
        try {
            boolean goback = false;
            File fl = null;
            if ((imagePath == null) || (imagePath.trim().length() <= 0)) {
                fl = new File(properties.getProperty("images.dir") + "noimage.jpg");
                goback = true;
            }
            if (!goback) {
                fl = ImageDispatcher.findFile(imagePath);
                if (fl == null) {
                    fl = new File(properties.getProperty("images.dir") + "noimage.jpg");
                }
                if (!fl.exists()) {
                    fl = new File(properties.getProperty("images.dir") + "noimage.jpg");
                }
 
            }
 
            FileInputStream fis = new FileInputStream(fl);
            bis = new BufferedInputStream(fis);
        } catch (Throwable e) {
            e.printStackTrace();
        }
 
        return bis;
    }
}