ke
2024-11-27 3c6a1e27bd0057f570f06808c0b2a54e51888aa5
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package cn.gistack.by.nettyUpload;
 
 
import cn.gistack.by.nettyUpload.config.NettyHttpConfig;
import cn.gistack.by.nettyUpload.utils.BeenUtils;
import cn.gistack.by.nettyUpload.utils.HttpResponseUtils;
import cn.gistack.by.nettyUpload.utils.UriUtils;
import cn.gistack.by.nettyUpload.utils.ExceptionUtils;
import cn.gistack.by.picUpload.entity.BaiyiPicEntity;
import cn.gistack.by.picUpload.service.BaiyiPicService;
import cn.gistack.resource.feign.IOssClient;
import cn.hutool.core.date.DateUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.http.multipart.DiskAttribute;
import io.netty.handler.codec.http.multipart.DiskFileUpload;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
 
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
 
import static cn.gistack.by.nettyUpload.utils.ExceptionUtils.getStackString;
 
/**
 * 文件上传处理类
 * @author chenhaijian
 * @date 2020-05-21 14:45
 */
@Slf4j
public class  HttpUploadFileHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
    private static final String UPLOAD_FILE_URL = "/postfile";
    // http请求中可选参数
    private static final String FILENAME_PARAM = "filename";
    // http请求中必选的参数
    private static final String FILE_EXTNAME_PARAM = "fileextname";
 
    private NettyHttpConfig nettyHttpConfig;
 
    private BaiyiPicService baiyiPicService;
 
    /**
     * 对象存储构建类
     */
 
    private   IOssClient iOssClient;
 
    static {
        // should delete file on exit (in normal exit)
        DiskFileUpload.deleteOnExitTemporaryFile = true;
        DiskFileUpload.baseDirectory = null; // system temp directory
        DiskAttribute.deleteOnExitTemporaryFile = true; // should delete file on
        // exit (in normal exit)
        DiskAttribute.baseDirectory = null; // system temp directory
 
 
    }
 
    public HttpUploadFileHandler(NettyHttpConfig nettyHttpConfig, IOssClient iOssClient,BaiyiPicService baiyiPicService) {
        this.nettyHttpConfig = nettyHttpConfig;
        this.iOssClient = iOssClient;
        this.baiyiPicService = baiyiPicService;
 
    }
 
 
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest fullRequest) throws Exception {
        System.err.println("fullRequest:::"+fullRequest);
        boolean isRequestValid = isValidHttpRequest(ctx, fullRequest);
        if (!isRequestValid) {
            log.warn("Discard invalid request.");
 
            //HttpResponseUtils.sendResponseJson(ctx, HttpResponseStatus.BAD_REQUEST, "验证失败");
            return;
        }
 
        // 解析出文件路径
//        String filePath = parseFilePath(fullRequest);
//        System.err.println(filePath);
//        RandomAccessFile file = getSaveFile(filePath);
//        file.seek(0);
//        file.write(ByteBufUtil.getBytes(fullRequest.content()));
//        file.close();
 
        String fileName = this.parseFileName(fullRequest);
 
 
 
        ByteArrayInputStream stream = new ByteArrayInputStream(ByteBufUtil.getBytes(fullRequest.content()));
        MockMultipartFile multipartFile
            = new MockMultipartFile("11", fileName, "application/octet-stream", stream);
 
 
 
        // 上传荆楚水库的文件地址
//        System.err.println("iOssClient::");
//        System.err.println(iOssClient.getClass().getName());
//        System.err.println(iOssClient);
 
        String picurl = iOssClient.uploadFile(fileName, multipartFile);
//        System.err.println("图片上传地址"+picurl);
        log.info("图片上传地址::"+picurl);
 
        // 请求参数放入实体中
        BaiyiPicEntity baiyiPicEntity = parseEntity(fullRequest);
        baiyiPicEntity.setPicUrl(picurl);
        baiyiPicEntity.setPicStatus(1);
 
        //baiyiPicEntity存入数据库
        baiyiPicService.inserpicData(baiyiPicEntity);
 
 
        HttpResponseUtils.sendResponseJson(ctx, HttpResponseStatus.OK, "上传成功");
    }
 
    private BaiyiPicEntity parseEntity(FullHttpRequest fullRequest) throws URISyntaxException {
        URI uri = UriUtils.getURI(fullRequest);
        Map<String, String> queryParams = UriUtils.getQueryParams(uri);
 
        String mnNo = queryParams.get("mnNo");
        String order = queryParams.get("order");
        String obTime = queryParams.get("obTime");
        String obDatestr = queryParams.get("obDate");
        Date obDate = DateUtil.parse(obDatestr, "yyyy-MM-dd HH:mm:ss");
 
        String stationIdstr = queryParams.get("stationId");
        Integer stationId = Integer.parseInt(stationIdstr);
 
        String stationName = queryParams.get("stationName");
        String deviceIdstr = queryParams.get("deviceId");
        Integer deviceId = Integer.parseInt(deviceIdstr);
 
        String deviceName = queryParams.get("deviceName");
        String receiveTypestr = queryParams.get("receiveType");
        Integer receiveType = Integer.parseInt(receiveTypestr);
 
        BaiyiPicEntity baiyiPicEntity = new BaiyiPicEntity();
        baiyiPicEntity.setMnNo(mnNo);
        baiyiPicEntity.setOrder(order);
        baiyiPicEntity.setDeviceId(deviceId);
        baiyiPicEntity.setObTime(obTime);
        baiyiPicEntity.setObDate(obDate);
        baiyiPicEntity.setReceiveType(receiveType);
        baiyiPicEntity.setStationId(stationId);
        baiyiPicEntity.setStationName(stationName);
        baiyiPicEntity.setDeviceName(deviceName);
 
        baiyiPicEntity.setCreatedAt(new Date());
 
 
        return  baiyiPicEntity;
    }
 
    public static MultipartFile convert(File file) throws IOException {
 
        FileInputStream input = new FileInputStream(file);
        MultipartFile multipartFile = new MockMultipartFile("file",
            file.getName(), "application/octet-stream", input);
 
        return multipartFile;
    }
 
    private String parseFilePath(FullHttpRequest fullRequest) throws URISyntaxException {
        String dirPath = this.nettyHttpConfig.getFileSaveRootPath();
        String fileName = this.parseFileName(fullRequest);
 
        return dirPath +  File.separator + fileName;
    }
 
    private RandomAccessFile getSaveFile(String filePath) {
        try {
            RandomAccessFile file = new RandomAccessFile(filePath, "rw");
            return file;
        } catch (Exception exception) {
            log.error(getStackString(exception));
            return null;
        }
    }
 
    private boolean isValidHttpRequest(ChannelHandlerContext ctx, FullHttpRequest request) {
        try {
            URI uri = UriUtils.getURI(request);
            log.info(uri.getPath());
            // authorization效验
            String authorization = request.headers().get("Authorization");
            if(!this.nettyHttpConfig.getAuthorization().equals(authorization)) {
                log.warn("Authorization校验失败!!");
                // 校验不通过
                HttpResponseUtils.sendResponseJson(ctx, HttpResponseStatus.UNAUTHORIZED, HttpResponseStatus.UNAUTHORIZED.toString());
                // 校验不通过
                return false;
            }
 
            // 密码校验
//            boolean authFlag = auth(ctx, uri);
//            if (!authFlag) {
//                // 校验不通过
//                HttpResponseUtils.sendResponseJson(ctx, HttpResponseStatus.UNAUTHORIZED, HttpResponseStatus.UNAUTHORIZED.toString());
//                // 校验不通过
//                return false;
//            }
 
            // uri 路径校验
//            if (!uri.getPath().startsWith(UPLOAD_FILE_URL)) {
//                HttpResponseUtils.sendResponseJson(ctx, HttpResponseStatus.BAD_REQUEST,"the url must starts with /postfile");
//                return false;
//            }
 
            return true;
        } catch (Exception exception) {
            exception.printStackTrace();
            log.error(getStackString(exception));
            return false;
        }
    }
 
    private String parseFileName(FullHttpRequest fullRequest) throws URISyntaxException {
        URI uri = UriUtils.getURI(fullRequest);
        Map<String, String> queryParams = UriUtils.getQueryParams(uri);
        try {
            if (queryParams.containsKey(FILENAME_PARAM)) {
                return queryParams.get(FILENAME_PARAM);
            } else {
                SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY_MM_dd_HH_MM_SS_sss");
                String filePureName = dateFormat.format(new Date());
                return filePureName + "." + queryParams.get(FILE_EXTNAME_PARAM);
            }
        } catch (Exception exception) {
            log.error(ExceptionUtils.getStackString(exception));
            return String.valueOf(System.currentTimeMillis());
        }
    }
 
    private void writeResponse(ChannelHandlerContext ctx, String context) {
        ByteBuf buf = Unpooled.copiedBuffer(context, CharsetUtil.UTF_8);
        FullHttpResponse response = new DefaultFullHttpResponse(
                HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf);
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html;charset=utf-8");
        //设置短连接 addListener 写完马上关闭连接
        ctx.channel().writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    }
 
    /**
     * 进行请求的密码校验
     * @param ctx
     * @param uri
     * @return
     */
    private boolean auth(ChannelHandlerContext ctx, URI uri) {
        Map<String, String> queryParams = UriUtils.getQueryParams(uri);
        String passwd = queryParams.get("passwd");
        if (this.nettyHttpConfig.getFileUpPasswd().equals(passwd)) {
            return true;
        }
        return false;
    }
 
    @Override
    public void channelInactive(ChannelHandlerContext ctx) {
        ctx.close();
    }
 
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error(getStackString(cause));
        cause.printStackTrace();
        ctx.close();
    }
}