wyyay 1 year ago
parent
commit
dd97a53c07

+ 82 - 0
business-service/src/main/java/com/ozs/service/utils/HttpGetUtil.java

@@ -0,0 +1,82 @@
+package com.ozs.service.utils;
+
+import com.ozs.common.utils.MinioUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Resource;
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+/**
+ * @author wyy
+ * @subject
+ * @creat 2023/4/20
+ */
+public class HttpGetUtil {
+    private static final Logger log = LoggerFactory.getLogger(HttpGetUtil.class);
+    @Resource
+    public static MinioUtils minioUtils;
+
+    public static void getPicture(URL url,String fileName) throws IOException {
+        HttpURLConnection connection = null;
+        BufferedInputStream bis = null;
+        BufferedOutputStream bos = null;
+        try {
+            connection = (HttpURLConnection) url.openConnection();
+            // http正文内,因此需要设为true
+            connection.setDoOutput(true);
+            connection.setDoInput(true);
+            // Post 请求不能使用缓存
+            connection.setUseCaches(false);
+            //设置本次连接是否自动重定向
+            connection.setInstanceFollowRedirects(true);
+
+            connection.setRequestMethod("GET");
+            connection.setRequestProperty("Accept-Language", "zh,zh-CN;q=0.9");
+            connection.setRequestProperty("connection", "keep-alive");
+            //设置参数类型是json格式
+            connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
+            connection.setRequestProperty("Accept","application/json");
+            //connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
+            connection.setRequestProperty("Charsert", "UTF-8");
+            //设置读取时间为30秒
+            connection.setConnectTimeout(30 * 1000);
+            connection.setReadTimeout(30 * 1000);
+            connection.connect();
+
+            // 返回流
+            System.out.println("responseCode=" + connection.getResponseCode());
+            log.info("responseCode="+connection.getResponseCode());
+            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
+                InputStream input = connection.getInputStream();
+                bis = new BufferedInputStream(input);
+                minioUtils.minIoClientUpload(input, fileName);
+//                FileOutputStream output = new FileOutputStream(fileName);
+//                bos = new BufferedOutputStream(output);
+//                byte[] buffer = new byte[1024];
+//                int size = 0;
+//                while ((size = bis.read(buffer, 0, 1024)) != -1) {
+//                    bos.write(buffer, 0, size);
+//                }
+//                bos.flush();
+            }
+        } catch (Exception e) {
+            //先关闭外层的缓冲流,再关闭内层的流,但是在关闭外层流的同时,
+            //内层流也会自动的进行关闭,关于内层流的关闭,可以省略
+            e.printStackTrace();
+        } finally {
+            if (bis != null) {
+                bis.close();
+            }
+//            if (bos != null) {
+//                bos.close();
+//            }
+            if (connection != null) {
+                connection.disconnect();
+            }
+//            os.close();
+        }
+    }
+}

+ 75 - 0
hazard-admin/src/main/java/com/ozs/web/controller/common/PictureController.java

@@ -0,0 +1,75 @@
+package com.ozs.web.controller.common;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.ozs.common.core.domain.AjaxResult;
+import com.ozs.common.utils.ApiTokenUtils;
+import com.ozs.common.utils.DateUtils;
+import com.ozs.common.utils.MinioUtils;
+import com.ozs.common.utils.uuid.IdUtils;
+import com.ozs.service.entity.BaseCameraManagement;
+import com.ozs.service.entity.MsgAlarm;
+import com.ozs.service.service.BaseCameraManagementService;
+import com.ozs.service.service.MsgAlarmService;
+import com.ozs.service.utils.HttpGetUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.util.ObjectUtils;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.List;
+
+/**
+ * @author wyy
+ * @subject
+ * @creat 2023/4/18
+ */
+@RestController
+@RequestMapping("/getPicture")
+public class PictureController {
+    private static final Logger log = LoggerFactory.getLogger(PictureController.class);
+    @Resource
+    private ApiTokenUtils apiTokenUtils;
+    @Resource
+    BaseCameraManagementService baseCameraManagementService;
+    @Resource
+    MsgAlarmService msgAlarmService;
+    public final long timeout = 10000;
+    /**
+     * 获取图片
+     *
+     * @param
+     * @return
+     */
+    @PostMapping("/api/snap")
+    public AjaxResult getPicture() throws IOException {
+        List<BaseCameraManagement> list = baseCameraManagementService.list();
+        list.forEach(l -> {
+            String uuid = IdUtils.fastSimpleUUID();
+            if(!ObjectUtils.isEmpty(l.getCameraCode()) &&!ObjectUtils.isEmpty(l.getChannel())){
+                QueryWrapper<MsgAlarm> queryWrapper = new QueryWrapper<MsgAlarm>();
+                queryWrapper.lambda().eq(MsgAlarm::getCameraCode,l.getCameraCode());
+                queryWrapper.lambda().eq(MsgAlarm::getIsLock,2);
+                List<MsgAlarm> msgAlarmList = msgAlarmService.list(queryWrapper);
+                try {
+                    System.out.println("http://124.70.58.209:18891/api/snap?stream="+l.getCameraCode()+"/"+l.getChannel()+"&timeout="+timeout);
+                    log.info("请求url"+"http://124.70.58.209:18891/api/snap?stream="+l.getCameraCode()+"/"+l.getChannel()+"&timeout="+timeout);
+                    URL url = new URL("http://124.70.58.209:18891/api/snap?stream="+l.getCameraCode()+"/"+l.getChannel()+"&timeout="+timeout);
+                    String fileName = "cameraPicture/"+l.getCameraCode()+"_"+uuid +".jpg";
+                    log.info("fileName="+fileName);
+                    HttpGetUtil.getPicture(url, fileName);
+                } catch (MalformedURLException e) {
+                    e.printStackTrace();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        });
+        return AjaxResult.success(list);
+    }
+}