Browse Source

截图定时优化

wyyay 10 months ago
parent
commit
fcede2629b

+ 9 - 0
business-service/src/main/java/com/ozs/service/service/impl/CameraCaptureService.java

@@ -1,8 +1,11 @@
 package com.ozs.service.service.impl;
 
 import com.ozs.common.utils.MinioUtils;
+import com.ozs.common.utils.uuid.IdUtils;
+import com.ozs.service.utils.FileUploader;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
@@ -21,6 +24,8 @@ public class CameraCaptureService {
     private static final Logger log = LoggerFactory.getLogger(CameraCaptureService.class);
     @Resource
     MinioUtils minioUtils;
+    @Autowired
+    FileUploader fileUploader;
 
     public void getCapture(URL url, String fileName) throws IOException {
         HttpURLConnection connection = null;
@@ -66,6 +71,10 @@ public class CameraCaptureService {
                 log.info("getCapture======" + fileName);
                 minioUtils.minIoClientUpload(byteArrayInputStream, fileName);
                 System.out.println("fileName"+fileName+"图片结束截图时间"+new Date());
+                String uploadUrl = "/data/test_picture/"+ IdUtils.fastSimpleUUID()+".jpeg";
+                //File targetVideoDir = new File(uploadUrl);
+                fileUploader.transfer(uploadUrl,bytes);
+
             }
         } catch (Exception e) {
             //先关闭外层的缓冲流,再关闭内层的流,但是在关闭外层流的同时,

+ 47 - 0
business-service/src/main/java/com/ozs/service/utils/FileUploader.java

@@ -0,0 +1,47 @@
+package com.ozs.service.utils;
+import org.springframework.stereotype.Service;
+
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+
+/**
+ * @author wyy
+ * @subject
+ * @creat 2024/3/8
+ */
+@Service
+public class FileUploader {
+    String uploadUrl = "/data/test_picture/"; // 服务器URL
+
+    public void transfer(String fileName, byte[] fileContent) throws Exception{
+        URL url = new URL(uploadUrl);
+        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+        connection.setDoOutput(true); // 允许输出
+        connection.setRequestMethod("POST"); // 设置请求方法为POST
+        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=AaB03x"); // 设置请求头,这里只是一个示例boundary
+
+        try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
+            // 构造multipart/form-data格式的请求体
+            String boundary = "AaB03x";
+            outputStream.writeBytes("--" + boundary + "\r\n");
+            outputStream.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName + "\"\r\n");
+            outputStream.writeBytes("Content-Type: application/octet-stream\r\n\r\n");
+
+            // 写入文件内容
+            outputStream.write(fileContent);
+            outputStream.writeBytes("\r\n--" + boundary + "--\r\n");
+
+            // 发送请求并获取响应
+            int responseCode = connection.getResponseCode();
+            if (responseCode == HttpURLConnection.HTTP_OK) {
+                System.out.println("File uploaded successfully!");
+            } else {
+                System.out.println("File upload failed with response code: " + responseCode);
+            }
+        } finally {
+            connection.disconnect(); // 断开连接
+        }
+    }
+}