Browse Source

截图定时优化

wyyay 10 months ago
parent
commit
a478c70ed4

+ 3 - 6
business-service/src/main/java/com/ozs/service/service/impl/CameraCaptureService.java

@@ -2,7 +2,7 @@ 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.apache.poi.util.IOUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -24,8 +24,6 @@ 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;
@@ -76,9 +74,8 @@ public class CameraCaptureService {
                     log.debug("fileName"+fileName+"图片结束截图时间"+new Date());
                 }
                 String uploadUrl = "/data/test_picture/"+ IdUtils.fastSimpleUUID()+".jpeg";
-                //File targetVideoDir = new File(uploadUrl);
-                fileUploader.transfer(uploadUrl,bytes);
-
+                byteArrayInputStream.reset();
+                IOUtils.copy(byteArrayInputStream, new FileOutputStream(uploadUrl));
             }
         } catch (Exception e) {
             //先关闭外层的缓冲流,再关闭内层的流,但是在关闭外层流的同时,

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

@@ -1,54 +0,0 @@
-package com.ozs.service.utils;
-import com.ozs.service.service.impl.CameraCaptureService;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-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 {
-    private static final Logger log = LoggerFactory.getLogger(FileUploader.class);
-    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 (log.isDebugEnabled()) {
-                log.debug("file transfer responseCode:"+responseCode);
-            }
-            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(); // 断开连接
-        }
-    }
-}