| | |
| | | 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.entity.XfByPic; |
| | | import cn.gistack.by.picUpload.entity.XfBySnRelation; |
| | | import cn.gistack.by.picUpload.service.BaiyiPicService; |
| | | import cn.gistack.resource.feign.IOssClient; |
| | | import cn.hutool.core.date.DateUtil; |
| | |
| | | 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.handler.codec.http.multipart.*; |
| | | import io.netty.util.CharsetUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | |
| | | import java.net.URISyntaxException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | 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; |
| | | |
| | |
| | | */ |
| | | @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 static final String UPLOAD_DATA_URL = "/termite/syncDeviceState"; |
| | | |
| | | /*** |
| | | * 新设备传图片接口 |
| | | */ |
| | | private static final String UPLOAD_FILE_URL = "/termite/termiteDataup"; |
| | | /** |
| | | * 旧设备传图片接口 |
| | | * 没有Authorization验证 |
| | | */ |
| | | private static final String UPLOAD_FILE_URL_OLD = "/termite/termiteDataupOld"; |
| | | /** |
| | | * * 旧设备sn码和物联网关系表 |
| | | * * 没有Authorization验证 |
| | | */ |
| | | private static final String UPLOAD_FILE_OLD_RELATION = "/termite/termiteRelation"; |
| | | |
| | | private NettyHttpConfig nettyHttpConfig; |
| | | |
| | |
| | | |
| | | |
| | | 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 urlyz = UriUtils.getURI(fullRequest).getPath(); |
| | | log.info("请求路径urlyz::"+urlyz); |
| | | |
| | | // 解析出文件路径 |
| | | // String filePath = parseFilePath(fullRequest); |
| | | // System.err.println(filePath); |
| | | // RandomAccessFile file = getSaveFile(filePath); |
| | | // file.seek(0); |
| | | // file.write(ByteBufUtil.getBytes(fullRequest.content())); |
| | | // file.close(); |
| | | if (UPLOAD_FILE_URL.equals(urlyz)) { //传图片 |
| | | // Authorization验证 |
| | | boolean isRequestValid = isValidHttpRequest(ctx, fullRequest); |
| | | if (!isRequestValid) { |
| | | HttpResponseUtils.sendResponseJson2(ctx, HttpResponseStatus.BAD_REQUEST, "验证失败",null); |
| | | return ; |
| | | } |
| | | |
| | | //String fileName = this.parseFileName(fullRequest); |
| | | XfByPic xfByPic = setEntity(fullRequest); |
| | | |
| | | String fileName = System.currentTimeMillis() + ".jpg"; |
| | | |
| | | ByteArrayInputStream stream = new ByteArrayInputStream(ByteBufUtil.getBytes(fullRequest.content())); |
| | | MockMultipartFile multipartFile |
| | | = new MockMultipartFile("11", fileName, "application/octet-stream", stream); |
| | | // 保存入库xfByPic |
| | | baiyiPicService.inserXfPicUrl(xfByPic); |
| | | |
| | | |
| | | |
| | | // 上传荆楚水库的文件地址 |
| | | // System.err.println("iOssClient::"); |
| | | // System.err.println(iOssClient.getClass().getName()); |
| | | // System.err.println(iOssClient); |
| | | }else if(UPLOAD_FILE_URL_OLD.equals(urlyz)) { // 旧类型设备上传图片数据接口 |
| | | log.info("UPLOAD_FILE_URL_OLD接口调用成功!!!!:"); |
| | | System.err.println(fullRequest); |
| | | |
| | | String picurl = iOssClient.uploadFile(fileName, multipartFile); |
| | | // System.err.println("图片上传地址"+picurl); |
| | | log.info("图片上传地址::"+picurl); |
| | | XfByPic xfByPic = setEntity(fullRequest); |
| | | // 保存入库xfByPic |
| | | baiyiPicService.inserXfOldPicUrl(xfByPic); |
| | | |
| | | // 请求参数放入实体中 |
| | | BaiyiPicEntity baiyiPicEntity = parseEntity(fullRequest); |
| | | baiyiPicEntity.setPicUrl(picurl); |
| | | baiyiPicEntity.setPicStatus(1); |
| | | |
| | | //baiyiPicEntity存入数据库 |
| | | baiyiPicService.inserpicData(baiyiPicEntity); |
| | | }else if(UPLOAD_FILE_OLD_RELATION.equals(urlyz)) { //传sn和rtu编码关系接口 |
| | | HttpPostMultipartRequestDecoder test = new HttpPostMultipartRequestDecoder(fullRequest); |
| | | List<InterfaceHttpData> bodyHttpDatas = test.getBodyHttpDatas(); |
| | | String sn = ""; |
| | | for (InterfaceHttpData item : bodyHttpDatas ) { |
| | | if ( item instanceof Attribute){ |
| | | Attribute attribute = (Attribute) item; |
| | | String value = attribute.getValue(); |
| | | switch (attribute.getName()) { |
| | | case "sn": |
| | | sn = value; |
| | | break; |
| | | } |
| | | |
| | | } |
| | | } |
| | | // 查询sn对应的 |
| | | XfBySnRelation xfBySnRelation = baiyiPicService.selectMnnoBySn(sn); |
| | | HashMap<String, Object> objectObjectHashMap = new HashMap<>(); |
| | | objectObjectHashMap.put("mn_no", xfBySnRelation.getWlwRtu()); |
| | | objectObjectHashMap.put("order", xfBySnRelation.getWlwOrder()); |
| | | |
| | | |
| | | HttpResponseUtils.sendResponseJson2(ctx, HttpResponseStatus.OK, "查询成功",objectObjectHashMap); |
| | | return ; |
| | | } else { |
| | | HttpResponseUtils.sendResponseJson2(ctx, HttpResponseStatus.BAD_REQUEST, "验证失败",null); |
| | | return ; |
| | | } |
| | | |
| | | HttpResponseUtils.sendResponseJson(ctx, HttpResponseStatus.OK, "上传成功"); |
| | | } |
| | | |
| | | private BaiyiPicEntity parseEntity(FullHttpRequest fullRequest) throws URISyntaxException { |
| | | URI uri = UriUtils.getURI(fullRequest); |
| | | Map<String, String> queryParams = UriUtils.getQueryParams(uri); |
| | | //请求参数注入实体 |
| | | public XfByPic setEntity(FullHttpRequest fullRequest) throws Exception { |
| | | HttpPostMultipartRequestDecoder test = new HttpPostMultipartRequestDecoder(fullRequest); |
| | | List<InterfaceHttpData> bodyHttpDatas = test.getBodyHttpDatas(); |
| | | XfByPic xfByPic = new XfByPic(); |
| | | for (InterfaceHttpData item : bodyHttpDatas ) { |
| | | if (item instanceof Attribute) { |
| | | Attribute attribute = (Attribute) item; |
| | | String value = attribute.getValue(); |
| | | switch (attribute.getName()) { |
| | | case "mnNo": |
| | | xfByPic.setMnNo(value); |
| | | break; |
| | | case "order": |
| | | |
| | | 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()); |
| | | xfByPic.setOrder(value); |
| | | break; |
| | | case "sn": |
| | | xfByPic.setSn(value); |
| | | break; |
| | | case "ap": |
| | | xfByPic.setAp(value); |
| | | break; |
| | | case "camera": |
| | | xfByPic.setCamera(value); |
| | | break; |
| | | case "hardware": |
| | | xfByPic.setHardware(value); |
| | | break; |
| | | case "rssi": |
| | | xfByPic.setRssi(value); |
| | | break; |
| | | case "software": |
| | | xfByPic.setSoftware(value); |
| | | break; |
| | | case "temperature": |
| | | xfByPic.setTemperature(value); |
| | | break; |
| | | case "voltage": |
| | | xfByPic.setVoltage(value); |
| | | break; |
| | | } |
| | | |
| | | |
| | | return baiyiPicEntity; |
| | | } else if (item instanceof FileUpload) { |
| | | |
| | | FileUpload file = (FileUpload) item; |
| | | String filename = file.getFilename(); |
| | | |
| | | ByteArrayInputStream stream = new ByteArrayInputStream(ByteBufUtil.getBytes(file.getByteBuf())); |
| | | MockMultipartFile multipartFile |
| | | = new MockMultipartFile("11", filename, "application/octet-stream", stream); |
| | | |
| | | String picurl = iOssClient.uploadFile(filename, multipartFile); |
| | | log.info("图片上传地址::" + picurl); |
| | | xfByPic.setImageurl(picurl); |
| | | } |
| | | } |
| | | xfByPic.setCreatedAt(new Date()); |
| | | |
| | | return xfByPic; |
| | | } |
| | | |
| | | |
| | | // 请求路径验证 |
| | | public String urlyz(ChannelHandlerContext ctx, FullHttpRequest request) { |
| | | try { |
| | | |
| | | URI uri = UriUtils.getURI(request); |
| | | System.err.println("uri.getPath()::::"+uri.getPath()); |
| | | System.err.println(""+uri.getPath()); |
| | | |
| | | if (uri.getPath().startsWith(UPLOAD_DATA_URL)) { |
| | | return UPLOAD_DATA_URL; |
| | | |
| | | } else if (uri.getPath().startsWith(UPLOAD_FILE_URL)) { |
| | | return UPLOAD_FILE_URL; |
| | | |
| | | } else if (uri.getPath().startsWith(UPLOAD_FILE_URL_OLD)) { |
| | | |
| | | return UPLOAD_FILE_URL_OLD; |
| | | } |
| | | } catch (Exception exception) { |
| | | exception.printStackTrace(); |
| | | log.error(getStackString(exception)); |
| | | return null; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | |
| | | public static MultipartFile convert(File file) throws IOException { |
| | | |
| | |
| | | private boolean isValidHttpRequest(ChannelHandlerContext ctx, FullHttpRequest request) { |
| | | try { |
| | | URI uri = UriUtils.getURI(request); |
| | | log.info(uri.getPath()); |
| | | // log.info(uri.getPath()); |
| | | // authorization效验 |
| | | String authorization = request.headers().get("Authorization"); |
| | | if(!this.nettyHttpConfig.getAuthorization().equals(authorization)) { |