420接收网闸文件服务(现场部署版)
Lius
2025-02-27 f06f695488a64dcc0945e282dffe120148f21dca
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package com.mm.controller;
 
import cn.hutool.crypto.SmUtil;
import com.mm.util.*;
import com.mm.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.method.HandlerMethod;
 
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.InetAddress;
import java.net.URLDecoder;
import java.net.UnknownHostException;
 
 
@RestController
@RequestMapping("/sys/api")
public class ServiceMdcController {
 
    @Value("${giveServiceTo.appIdCheck}")
    private String appIdCheck;
 
    @Value("${giveServiceTo.passwordCheck}")
    private String passwordCheck;
    @Value("${giveServiceTo.logIp}")
    private String logIp;
    @Value("${giveServiceTo.logPort}")
    private String logPort;
 
    @Value("${giveServiceTo.serverIp}")
    private String serverIp;
 
    @Value("${giveServiceTo.clientIp}")
    private String clientIp;
    @Value("${fileNumPath}")
    private String fileNumPath;
 
 
    @PostMapping(value = "/appAuth")
    public TokenResp getToken(@RequestBody PostParams postParams) {
 
        //获取请求头中传入的appId和password
        String appId = postParams.getAppId();
        String password = postParams.getPassword();
 
        //用于校验的正确的appId和password(根据业务调整)
        /*String appIdCheck = "admin";
        String passwordCheck = "123";*/
 
        if (appIdCheck.equals(appId)) {
            //校验通过,生成token签名
            if (SM3Util.verify(passwordCheck, password)) {
                String token = JwTUtil.sign(appId, passwordCheck);
                return new TokenResp("200", "认证成功", token);
            } else {
                return new TokenResp("101", "密码错误", null);
            }
        } else {
            return new TokenResp("101", "账号错误", null);
        }
 
//        //校验appId和paaword,由于测试用的明文密码,SM3Util.verify()先将铭文密码SM3加密后再与传入password对比
//        if (appIdCheck.equals(appId) && SM3Util.verify(passwordCheck, password)) {
//            //校验通过,生成token签名
//            String token = JwTUtil.sign(appId, passwordCheck);
//            return new TokenResp("200", "认证成功", token);
//        } else {
//            //to-do认证失败
//            return null;
//        }
    }
 
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
//        //如果不是映射到方法直接通过
//        if (!(handler instanceof HandlerMethod)) {
//            return true;
//        }
//        //从 http 请求头中取出 token
//        String token = request.getHeader("Token");
//        if (token == null) {
//            throw new RuntimeException("无 token ,请重新登陆");
//        }
//        /*//验证 token,JwtUtil为生成token和验证token的工具类
//        JwTUtil.checkSign(token);
//
//        //获取 token 中的 appId
//        String appId = JwTUtil.getUserId(token);
//        //获取 token 中的其他数据
//        Map<String, Object> info = JwTUtil.getInfo(token);
//        info.forEach((k, v) -> System.out.println(k + ":" + v));*/
//        return true;
//    }
 
    @PostMapping(value = "/fileUpload")
    public RespData uploadFiles(HttpServletRequest request) {
        SysLogTypeObjectDto objectName = new SysLogTypeObjectDto();
        objectName.setDateTime(DateUtil.format(DateUtil.getNow(), DateUtil.STR_DATE_TIME_SMALL));
 
        FileDetail fileDetail = new FileDetail();
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //获取FileName
        if (request.getHeader("FileName") != null) {
            try {
                fileDetail.setFileName(URLDecoder.decode(request.getHeader("FileName"), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        //获取Content-Length
        if (request.getHeader("Content-Length") != null) {
            try {
                fileDetail.setContentLength(URLDecoder.decode(request.getHeader("Content-Length"), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        //获取文件流
        ServletInputStream inputStream = null;
        try {
            inputStream = request.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        if (inputStream == null) {
            System.out.println("inputStream为空");
        }
 
        //操作文件流,上传文件
        //fileService.uploadFile(inputStream,fileDetail.getFileName());
        //DataInputStream in = new DataInputStream(inputStream);
        FileOutputStream fileOut = null;
        try {
            fileOut = new FileOutputStream(fileDetail.getFileName());//这里可以改路径
            IOUtils.copy(inputStream, fileOut);
            fileOut.flush();
//            fileOut.flush();
            objectName.setResult("成功");
 
            File file = new File(fileDetail.getFileName());
            objectName.setFileSize(file.getName());
            objectName.setDateTime(DateUtil.format(DateUtil.getNow(), DateUtil.STR_DATE_TIME_SMALL));
            objectName.setFileSize(FileUtil.changeFileFormatKb(String.valueOf(file.length())));
            objectName.setDestination(serverIp);//服务端IP
            objectName.setSourceAddress(clientIp);//客户端
            String sm3 = SmUtil.sm3(file);
            objectName.setAbstract1(sm3);
 
            //不记录数据量
 
            String fileNum = FileNumUtil.readFileSum(fileNumPath);
            if (StringUtils.isBlank(fileNum)) {
                objectName.setFileNum(Integer.toString(1));
                FileNumUtil.fileWriterSql(Integer.toString(1), fileNumPath);
            } else {
                Integer num = Integer.valueOf(fileNum) + 1;
                objectName.setFileNum(Integer.toString(num));
                FileNumUtil.fileWriterSql(Integer.toString(num), fileNumPath);
            }
            InetAddress address = null;
            try {
                address = InetAddress.getLocalHost();
                String ip = address.getHostAddress();
                objectName.setAddress(ip);
            } catch (UnknownHostException e) {
                objectName.setAddress("20.10.17.19");
            }
            try {
                objectName.setTypes("Info");
                SyslogClient.sendClient(logIp, Integer.valueOf(logPort), objectName.toString());
            } catch (Exception e) {
 
            }
 
        } catch (IOException e) {
            objectName.setResult("失败");
            objectName.setTypes("error");
            SyslogClient.sendClient(logIp, Integer.valueOf(logPort), objectName.toString());
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (fileOut != null) {
                    fileOut.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new RespData("200", "文件接收成功");
    }
}