From 55793861746f0a8dd74b84d444a60c43f982b255 Mon Sep 17 00:00:00 2001
From: zhanghua <314079846@qq.com>
Date: 星期四, 06 二月 2025 23:17:38 +0800
Subject: [PATCH] oss获取图方式修改
---
ycl-platform/src/main/java/com/ycl/task/SynchronizeDHAlarm.java | 2
ycl-platform/src/main/java/com/ycl/controller/platformApi/AlarmController.java | 69 ++++++++++++++---
ycl-platform/src/test/java/com/ycl/sccgplatform/Test1.java | 157 +++++++++++++++++++++++++++++++++------
3 files changed, 191 insertions(+), 37 deletions(-)
diff --git a/ycl-platform/src/main/java/com/ycl/controller/platformApi/AlarmController.java b/ycl-platform/src/main/java/com/ycl/controller/platformApi/AlarmController.java
index d58060a..e3ea3f3 100644
--- a/ycl-platform/src/main/java/com/ycl/controller/platformApi/AlarmController.java
+++ b/ycl-platform/src/main/java/com/ycl/controller/platformApi/AlarmController.java
@@ -2,6 +2,14 @@
import cn.hutool.json.JSONConverter;
import com.alibaba.fastjson.JSONObject;
+import com.aliyun.oss.ClientBuilderConfiguration;
+import com.aliyun.oss.OSS;
+import com.aliyun.oss.OSSClientBuilder;
+import com.aliyun.oss.OSSException;
+import com.aliyun.oss.common.auth.CredentialsProviderFactory;
+import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
+import com.aliyun.oss.common.comm.SignVersion;
+import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ycl.annotation.LogSave;
@@ -13,6 +21,7 @@
import com.ycl.service.video.IVideoAlarmReportService;
import com.ycl.service.video.impl.IVideoPointService;
import com.ycl.util.VideoUtil;
+import com.ycl.utils.ConstantPropertiesUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
@@ -29,10 +38,7 @@
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
-import java.util.Base64;
-import java.util.Dictionary;
-import java.util.Hashtable;
-import java.util.List;
+import java.util.*;
/**
* @author admin
@@ -151,19 +157,58 @@
}
+ private URL translateOSSUrl(String fileUrl) {
+ OSS ossClient = null;
+ try {
+ String endpoint = ConstantPropertiesUtils.END_POINT;
+ String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
+ String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
+ String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
+
+ String[] urlArray = fileUrl.split("Expires=");
+
+ String[] urlArray2 = urlArray[0].split("aliyuncs.com/");
+
+ String objectName = urlArray2[1].replace("?", "");
+
+ ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
+
+ // 璁剧疆绛惧悕URL杩囨湡鏃堕棿锛屽崟浣嶄负姣銆傛湰绀轰緥浠ヨ缃繃鏈熸椂闂翠负1灏忔椂涓轰緥銆�
+ Date expiration = new Date(new Date().getTime() + 3600 * 1000L);
+ // 鐢熸垚浠ET鏂规硶璁块棶鐨勭鍚峌RL銆傛湰绀轰緥娌℃湁棰濆璇锋眰澶达紝鍏朵粬浜哄彲浠ョ洿鎺ラ�氳繃娴忚鍣ㄨ闂浉鍏冲唴瀹广��
+ URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
+
+ return url;
+ } catch (OSSException oe) {
+ System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ + "but was rejected with an error response for some reason.");
+ System.out.println("Error Message:" + oe.getErrorMessage());
+ System.out.println("Error Code:" + oe.getErrorCode());
+ System.out.println("Request ID:" + oe.getRequestId());
+ System.out.println("Host ID:" + oe.getHostId());
+ } finally {
+ if (ossClient != null) {
+ ossClient.shutdown();
+ }
+ }
+ return null;
+ }
+
private byte[] getBytes(String fileUrl, String OSSAccessKeyId, String Signature, String sessionId) {
int index = sessionDic.get(sessionId);
try {
if (sessionDic.get(sessionId) < 10) {
sessionDic.remove(sessionId);
- fileUrl = fileUrl.replace("Expires", "v");
-// System.out.println("getImages鑾峰彇鍥剧墖鍦板潃锛�" + fileUrl + "&OSSAccessKeyId=" + OSSAccessKeyId + "&Signature=" + Signature);
- URL url = null;
- if (StringUtils.isNotBlank(OSSAccessKeyId) && StringUtils.isNotBlank(Signature)) {
- url = new URL(fileUrl + "&OSSAccessKeyId=" + OSSAccessKeyId + "&Signature=" + Signature);
- } else {
- url = new URL(fileUrl);
- }
+ URL url = translateOSSUrl(fileUrl);
+// fileUrl = fileUrl.replace("Expires", "v");
+//// System.out.println("getImages鑾峰彇鍥剧墖鍦板潃锛�" + fileUrl + "&OSSAccessKeyId=" + OSSAccessKeyId + "&Signature=" + Signature);
+// URL url = null;
+// if (StringUtils.isNotBlank(OSSAccessKeyId) && StringUtils.isNotBlank(Signature)) {
+// url = new URL(fileUrl + "&OSSAccessKeyId=" + OSSAccessKeyId + "&Signature=" + Signature);
+// } else {
+// url = new URL(fileUrl);
+// }
+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(10 * 1000);
diff --git a/ycl-platform/src/main/java/com/ycl/task/SynchronizeDHAlarm.java b/ycl-platform/src/main/java/com/ycl/task/SynchronizeDHAlarm.java
index 7ad52dd..9f88d04 100644
--- a/ycl-platform/src/main/java/com/ycl/task/SynchronizeDHAlarm.java
+++ b/ycl-platform/src/main/java/com/ycl/task/SynchronizeDHAlarm.java
@@ -51,7 +51,7 @@
if (videoAlarmReport.getPicByte() != null) {
String extension = "jpg";
InputStream inputStream = new ByteArrayInputStream(videoAlarmReport.getPicByte());
- String picData = ossService.uploadImages(inputStream, extension);
+ String picData = ossService.uploadImages(inputStream, extension,0);
videoAlarmReport.setPicData(picData);
}
diff --git a/ycl-platform/src/test/java/com/ycl/sccgplatform/Test1.java b/ycl-platform/src/test/java/com/ycl/sccgplatform/Test1.java
index 21f988b..148d96b 100644
--- a/ycl-platform/src/test/java/com/ycl/sccgplatform/Test1.java
+++ b/ycl-platform/src/test/java/com/ycl/sccgplatform/Test1.java
@@ -1,11 +1,20 @@
package com.ycl.sccgplatform;
+import com.aliyun.oss.ClientBuilderConfiguration;
+import com.aliyun.oss.OSS;
+import com.aliyun.oss.OSSClientBuilder;
+import com.aliyun.oss.OSSException;
+import com.aliyun.oss.common.auth.CredentialsProviderFactory;
+import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
+import com.aliyun.oss.common.comm.SignVersion;
+import com.aliyuncs.exceptions.ClientException;
import com.dahua.netsdk.lib.callback.impl.AnalyzerDataCB;
import com.ycl.common.util.UtilNumber;
import com.ycl.entity.dingding.DingUserInfo;
import com.ycl.entity.user.UmsAdmin;
import com.ycl.mapper.unlawful.UnlawfulMapper;
import com.ycl.service.oss.OssService;
+import com.ycl.utils.ConstantPropertiesUtils;
import net.coobird.thumbnailator.Thumbnails;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -33,28 +42,128 @@
@Test
void test() {
+ String fileUrl = "https://huachuang-resource.oss-cn-beijing.aliyuncs.com/瀹跺涵鏁欒偛/涓浗闄惰鐭ュ崗浼氬搴暀鑲蹭笓濮斾細.doc?Expires=1738857178&OSSAccessKeyId=LTAIxtcfDe3fgfcf&Signature=wTwx6JA6VWbPB0skVySy9MCexog=";
+ // http://zhzfptoss.oss-cn-zjls-lszwy-d01-a.ops.zjlscloud.com/sczf/2023062950465224.jpg?Expires=2634106907&OSSAccessKeyId=Tkf72Q1gdzETsZf6&Signature=ttME%2BeFhss8wPbkRCxAsV%2FPNWGQ%3D
+
+ OSS ossClient = null;
+ try {
+
+ String endpoint = "https://oss-cn-beijing.aliyuncs.com";
+ String accessKeyId = "LTAIxtcfDe3fgfcf";
+ String accessKeySecret = "xHxLVU5dm7USdJuQaCfp1hpcUU1u8v";
+ String bucketName = "huachuang-resource";
+
+ String[] urlArray = fileUrl.split("Expires=");
+
+ String[] urlArray2 = urlArray[0].split("aliyuncs.com/");
+
+ String objectName = urlArray2[1].replace("?", "");
+
+
+ ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
+
+
+ // 璁剧疆绛惧悕URL杩囨湡鏃堕棿锛屽崟浣嶄负姣銆傛湰绀轰緥浠ヨ缃繃鏈熸椂闂翠负1灏忔椂涓轰緥銆�
+ Date expiration = new Date(new Date().getTime() + 3600 * 1000L);
+ // 鐢熸垚浠ET鏂规硶璁块棶鐨勭鍚峌RL銆傛湰绀轰緥娌℃湁棰濆璇锋眰澶达紝鍏朵粬浜哄彲浠ョ洿鎺ラ�氳繃娴忚鍣ㄨ闂浉鍏冲唴瀹广��
+ URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
+ System.out.println(url);
+
+//
+// // http://zhzfptoss.oss-cn-zjls-lszwy-d01-a.ops.zjlscloud.com/sczf/2025020620664017jpg?v=2684887407&OSSAccessKeyId=Tkf72Q1gdzETsZf6&Signature=88dn7R/gDdqxWUCqZdCi/NgSXg8=
+// OSS ossClient = null;
+// try {
+// // 浠ュ崕涓�1锛堟澀宸烇級鐨勫缃慐ndpoint涓轰緥锛屽叾瀹僐egion璇锋寜瀹為檯鎯呭喌濉啓銆�
+// String endpoint = "https://oss-cn-beijing.aliyuncs.com";
+// // 浠庣幆澧冨彉閲忎腑鑾峰彇璁块棶鍑瘉銆傝繍琛屾湰浠g爜绀轰緥涔嬪墠锛岃纭繚宸茶缃幆澧冨彉閲廜SS_ACCESS_KEY_ID鍜孫SS_ACCESS_KEY_SECRET銆�
+//// EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
+// // 濉啓Bucket鍚嶇О锛屼緥濡俥xamplebucket銆�
+// String bucketName = "huachuang-resource";
+// // 濉啓Object瀹屾暣璺緞锛屼緥濡俥xampleobject.txt銆侽bject瀹屾暣璺緞涓笉鑳藉寘鍚獴ucket鍚嶇О銆�
+// String objectName = "瀹跺涵鏁欒偛/涓浗闄惰鐭ュ崗浼氬搴暀鑲蹭笓濮斾細.doc";
+// // 濉啓Bucket鎵�鍦ㄥ湴鍩熴�備互鍗庝笢1锛堟澀宸烇級涓轰緥锛孯egion濉啓涓篶n-hangzhou銆�
+// String region = "cn-beijing";
+// String accessKeyId = "LTAIxtcfDe3fgfcf";
+// String accessKeySecret = "xHxLVU5dm7USdJuQaCfp1hpcUU1u8v";
+// // 鍒涘缓OSSClient瀹炰緥銆�
+// ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
+// clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
+//
+// ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
+//
+//
+// // 璁剧疆绛惧悕URL杩囨湡鏃堕棿锛屽崟浣嶄负姣銆傛湰绀轰緥浠ヨ缃繃鏈熸椂闂翠负1灏忔椂涓轰緥銆�
+// Date expiration = new Date(new Date().getTime() + 3600 * 1000L);
+// // 鐢熸垚浠ET鏂规硶璁块棶鐨勭鍚峌RL銆傛湰绀轰緥娌℃湁棰濆璇锋眰澶达紝鍏朵粬浜哄彲浠ョ洿鎺ラ�氳繃娴忚鍣ㄨ闂浉鍏冲唴瀹广��
+// URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
+//
+//
+// try {
+// HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+// conn.setRequestMethod("GET");
+// conn.setConnectTimeout(10 * 1000);
+// conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
+//
+// InputStream input = conn.getInputStream();// 閫氳繃杈撳叆娴佽幏鍙栧浘鐗囨暟鎹�
+//
+// FileOutputStream downloadFile = new FileOutputStream("D://1.doc");
+//
+// int index;
+// byte[] bytes = new byte[1024];
+//
+// while ((index = input.read(bytes)) != -1) {
+// downloadFile.write(bytes, 0, index);
+// downloadFile.flush();
+// }
+// downloadFile.close();
+// input.close();
+
+//
+// ByteArrayOutputStream outs = new ByteArrayOutputStream();
+// Thumbnails.of(in).scale(0.6).outputQuality(1).toOutputStream(outs);
+// InputStream ins = new ByteArrayInputStream(outs.toByteArray());
+// String picData = ossService.uploadImages(ins, ".png", 0);
+// System.out.println(picData);
+
+
+ } catch (
+ OSSException oe) {
+ System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ + "but was rejected with an error response for some reason.");
+ System.out.println("Error Message:" + oe.getErrorMessage());
+ System.out.println("Error Code:" + oe.getErrorCode());
+ System.out.println("Request ID:" + oe.getRequestId());
+ System.out.println("Host ID:" + oe.getHostId());
+ } finally {
+ if (ossClient != null) {
+ ossClient.shutdown();
+ }
+ }
+
+ }
+
// List<CategoryDto> data = unlawfuldao.getDataByType(0, 5,"2022-12-14 17:19:01", "2022-12-14 17:19:03");
// System.out.println(data);
- String url = "http://test.xshcs.com/10%E6%9C%88%E6%8E%92%E6%9C%9F.jpg";
- try {
- HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
- connection.setReadTimeout(5000);
- connection.setConnectTimeout(5000);
- connection.setRequestMethod("GET");
- if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
- InputStream in = connection.getInputStream();
- ByteArrayOutputStream outs = new ByteArrayOutputStream();
- Thumbnails.of(in).scale(0.6).outputQuality(1).toOutputStream(outs);
- InputStream ins = new ByteArrayInputStream(outs.toByteArray());
- String picData = ossService.uploadImages(ins, ".png",0);
- System.out.println(picData);
- }
- } catch (IOException e) {
- System.out.println("鑾峰彇缃戠粶鍥剧墖鍑虹幇寮傚父锛屽浘鐗囪矾寰勪负锛�" + url);
-
- e.printStackTrace();
- }
- }
+// String url = "http://test.xshcs.com/10%E6%9C%88%E6%8E%92%E6%9C%9F.jpg";
+// try {
+// HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
+// connection.setReadTimeout(5000);
+// connection.setConnectTimeout(5000);
+// connection.setRequestMethod("GET");
+// if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
+// InputStream in = connection.getInputStream();
+// ByteArrayOutputStream outs = new ByteArrayOutputStream();
+// Thumbnails.of(in).scale(0.6).outputQuality(1).toOutputStream(outs);
+// InputStream ins = new ByteArrayInputStream(outs.toByteArray());
+// String picData = ossService.uploadImages(ins, ".png",0);
+// System.out.println(picData);
+// }
+// } catch (IOException e) {
+// System.out.println("鑾峰彇缃戠粶鍥剧墖鍑虹幇寮傚父锛屽浘鐗囪矾寰勪负锛�" + url);
+//
+// e.printStackTrace();
+// }
+//}
// @Test
// void test1(){
@@ -86,7 +195,7 @@
String PicData = br.readLine();
byte[] data = DatatypeConverter.parseBase64Binary(PicData);
InputStream inputStream = new ByteArrayInputStream(data);
- String s = ossService.uploadImages(inputStream, extension,0);
+ String s = ossService.uploadImages(inputStream, extension, 0);
System.out.println(s);
br.close();
@@ -94,13 +203,13 @@
fis.close();
}
- @Autowired
- AnalyzerDataCB analyzerDataCB;
+// @Autowired
+// AnalyzerDataCB analyzerDataCB;
@Test
void alarmTest() {
- analyzerDataCB.invoke(null, 0, null, null, 0, null, 0, null);
+// analyzerDataCB.invoke(null, 0, null, null, 0, null, 0, null);
// AlarmDataCB cbMessage = new AlarmDataCB();
// String m_strIp = "172.28.194.177";
--
Gitblit v1.8.0