|
@@ -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(); // 断开连接
|
|
|
- }
|
|
|
- }
|
|
|
-}
|