CZT
2023-11-27 c206acfaedc69c390fb67daa81bc686f58a212ef
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.ld.igds.basic.controller;
 
import com.ld.igds.basic.manager.FileManager;
import com.ld.igds.data.PageResponse;
 
import lombok.extern.slf4j.Slf4j;
 
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import java.io.*;
import java.net.URLEncoder;
 
/**
 * 文件上传接口
 *
 * @author
 */
@Slf4j
@RestController
@RequestMapping("basic/file")
public class FileController {
 
    @Autowired
    private FileManager fileManager;
 
    /**
     * 长传 出入库图片
     *
     * @param file
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/update-file")
    public PageResponse<String> updateAreationPos(
            @RequestParam(value = "file", required = true) MultipartFile file,
            HttpServletRequest request) throws IOException {
 
        String plateNum = request.getParameter("plateNum");
 
        return fileManager.upLoadInoutImg(file, plateNum);
    }
 
    /**
     * 下载从临时目录下载,需要项目名称和当前组织
     *
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/download-temp")
    public int downloadTemp(
            @RequestParam(value = "fileName", required = false) String fileName,
            @RequestParam(value = "companyId", required = false) String companyId,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException {
 
        if (StringUtils.isEmpty(fileName) || StringUtils.isEmpty(companyId)) {
            response.sendError(404, "缺少下载参数条件,无法执行下载。");
            return 404;
        }
        File file = fileManager.getTempFilePath(fileName, companyId);
 
        if (!file.exists()) {
            response.sendError(404, "没有获取到需要下载的文件,可能已被删除");
            return 404;
        }
 
        response.setContentType("application/octet-stream");
        response.setHeader("content-type", "application/octet-stream");
        response.setHeader("Content-Length", String.valueOf(file.length()));
        response.setHeader("Content-Disposition", "attachment;fileName="
                + URLEncoder.encode(fileName, "utf8"));
        byte[] buffer = new byte[1024];
 
        OutputStream os;
        try (FileInputStream fis = new FileInputStream(file);
                BufferedInputStream bis = new BufferedInputStream(fis)) {
            os = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer);
                i = bis.read(buffer);
            }
 
            fis.close();
            bis.close();
            os.close();
            os.flush();
        } catch (Exception e) {
            response.sendError(500, "文件下载出错:" + e.getMessage());
 
            log.error("------------文件下载失败--{}", e);
            return 500;
        }
 
        return 200;
    }
 
    /**
     * 文件流获取图片显示到页面--针对仓内抓拍
     *
     * @param fileName
     * @param timeStr
     *            抓拍的年月日时分秒 yyyyMMdd
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/get-snap-depot", method = RequestMethod.GET, produces = { "application/vnd.ms-excel;charset=UTF-8" })
    public String getDepotSnap(String fileName, String timeStr,
            HttpServletResponse response) throws IOException {
 
        // 设置返回内容格式
        response.setContentType("image/jpeg/jpg/png/gif/bmp/tiff/svg");
 
        String filePath;
        // 创建一个输入流
        InputStream in = null;
        // 创建输出流
        OutputStream os = null;
        try {
            if (null == fileName || null == timeStr) {
                filePath = fileManager.getFailImg();
            }else{
                filePath = fileManager.getSnapFilePath(fileName, timeStr);
                
                File file = new File(filePath);
                
                if(file.exists()){
                    log.debug("----抓拍图片地址-------{}", filePath);
                }else{
                    filePath = fileManager.getFailImg();
                    log.debug("----未获取到图纸,默认地址-------{}", filePath);
                }
            }
 
            // 用该文件创建一个输入流
            in = new FileInputStream(filePath);
            // 创建输出流
            os = response.getOutputStream();
            byte[] b = new byte[1024];
            while (in.read(b) != -1) {
                os.write(b);
            }
            in.close();
            os.close();
 
            return null;
 
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (null != in) {
                in.close();
            }
            if (null != os) {
                os.close();
            }
        }
    }
 
}