YYC
2025-08-27 0a01cc16996c5f46266337744324d5b7916392ba
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
package com.fzzy.api.service;
 
import com.fzzy.api.entity.Api1109;
import com.fzzy.api.view.repository.Api1109Rep;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestMethod;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
 
/**
 * @author vince.xu
 * @Title: ImgService
 * @ProjectName igds-api
 * @Description: TODO
 * @date 2022-9-611:25
 */
@Slf4j
@Controller
@RequestMapping
 
public class ImgService {
 
    @Autowired
    private Api1109Rep api1109Rep;
 
    /**
     * IO流读取图片 by:long
     * @return
     */
    @RequestMapping(value = "/img/{imgId}", method = RequestMethod.GET)
    public String IoReadImage(@PathVariable String imgId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletOutputStream out = null;
        FileInputStream ips = null;
        try {
            //获取图片存放路径
            Api1109 api1109 = api1109Rep.findById(imgId).get();
            ips = new FileInputStream(new File(api1109.getWjdz()));
            response.setContentType("multipart/form-data");
            out = response.getOutputStream();
            //读取文件流
            int len = 0;
            byte[] buffer = new byte[1024 * 10];
            while ((len = ips.read(buffer)) != -1){
                out.write(buffer,0,len);
            }
            out.flush();
        }
        catch (Exception e){
            log.error(e.getMessage(),e);
        }
        finally {
            if(out != null )
            out.close();
            if(ips != null )
            ips.close();
        }
        return null;
    }
}