From ee357f0dd7bee415a5eca5ba95f70eaeeb1aadcd Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: Thu, 20 Nov 2025 09:06:04 +0800
Subject: [PATCH] 优化
---
ard-work/pom.xml | 7
ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java | 5
ard-work/src/main/java/com/ard/work/component/forest/ErrorInterceptor.java | 26 +++
ard-work/src/main/java/com/ard/work/service/impl/ArdFlightTaskServiceServiceImpl.java | 299 +++++++++++++++++++++++-------------------
ard-work/src/main/java/com/ard/work/component/forest/TokenInterceptor.java | 16 +-
ard-work/src/main/java/com/ard/work/component/forest/TyClient.java | 3
ard-oss/src/main/java/com/ard/oss/service/impl/OssServiceContext.java | 36 +---
ard-work/src/main/java/com/ard/work/service/impl/ArdDroneInfoServiceImpl.java | 2
ard-work/src/main/java/com/ard/work/service/impl/ArdServerInfoServiceImpl.java | 19 +-
9 files changed, 231 insertions(+), 182 deletions(-)
diff --git a/ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java b/ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java
index 4d9065c..e9b3f96 100644
--- a/ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java
+++ b/ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java
@@ -46,7 +46,7 @@
}
- public CredentialsDTO getCredentials1111() {
+ public CredentialsDTO getCredentials111() {
try {
AssumeRoleProvider provider = new AssumeRoleProvider(endpoint, accessKey,
secretKey, Math.toIntExact(OssConfiguration.expire),
@@ -72,7 +72,7 @@
public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; }
}
};
- SSLContext sslContext = SSLContext.getInstance("TLS");
+ SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, trustAllCerts, new SecureRandom());
OkHttpClient httpClient = new OkHttpClient.Builder()
.sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustAllCerts[0])
@@ -179,6 +179,7 @@
}
}
}
+
@Override
public void putObject(String bucket, String objectKey, InputStream input, String contentType) {
try {
diff --git a/ard-oss/src/main/java/com/ard/oss/service/impl/OssServiceContext.java b/ard-oss/src/main/java/com/ard/oss/service/impl/OssServiceContext.java
index e78de05..2ee3fdc 100644
--- a/ard-oss/src/main/java/com/ard/oss/service/impl/OssServiceContext.java
+++ b/ard-oss/src/main/java/com/ard/oss/service/impl/OssServiceContext.java
@@ -140,13 +140,7 @@
// 上传文件到 Minio
this.ossService.putObject(bucket, objectName, inputStream, contentType);
- // ✅ 拼接永久 URL(不带 presigned 过期参数)
- String baseUrl = OssConfiguration.endpoint;
- if (!baseUrl.endsWith("/")) {
- baseUrl += "/";
- }
-
- return baseUrl + bucket + "/" + objectName;
+ return buildUrl(bucket, objectName);
} catch (Exception e) {
throw new IOException("Upload failed: " + e.getMessage(), e);
}
@@ -154,32 +148,19 @@
public String upload(String bucket, String objectKey, InputStream input) {
try {
-
// 上传文件
this.ossService.putObject(bucket, objectKey, input);
-
- // ✅ 构建永久访问 URL
- String baseUrl = OssConfiguration.endpoint;
- if (!baseUrl.endsWith("/")) {
- baseUrl += "/";
- }
- return baseUrl + bucket + "/" + objectKey;
+ return buildUrl(bucket, objectKey);
} catch (Exception e) {
throw new RuntimeException("Upload failed: " + e.getMessage(), e);
}
}
- @Async("threadPoolTaskExecutor")
+
public String upload(String bucket, String objectKey, InputStream input, String contentType) {
try {
// 上传文件时指定 Content-Type
this.ossService.putObject(bucket, objectKey, input, contentType);
-
- // ✅ 构建永久访问 URL
- String baseUrl = OssConfiguration.endpoint;
- if (!baseUrl.endsWith("/")) {
- baseUrl += "/";
- }
- return baseUrl + bucket + "/" + objectKey;
+ return buildUrl(bucket, objectKey);
} catch (Exception e) {
throw new RuntimeException("Upload failed: " + e.getMessage(), e);
}
@@ -216,4 +197,13 @@
}
}
+ // 构建url
+ private String buildUrl(String bucket, String key) {
+ String base = OssConfiguration.endpoint;
+ if (!base.endsWith("/")) {
+ base += "/";
+ }
+ return base + bucket + "/" + key;
+ }
+
}
diff --git a/ard-work/pom.xml b/ard-work/pom.xml
index 6ec0789..8424eb8 100644
--- a/ard-work/pom.xml
+++ b/ard-work/pom.xml
@@ -113,15 +113,12 @@
<groupId>com.ard</groupId>
<artifactId>ard-oss</artifactId>
</dependency>
- <!-- JavaCV 核心(非常小) -->
+ <!-- JavaCV -->
<dependency>
<groupId>org.bytedeco</groupId>
- <artifactId>javacv</artifactId>
+ <artifactId>javacv-platform</artifactId>
<version>1.5.8</version>
</dependency>
-
-
-
</dependencies>
<repositories>
<repository>
diff --git a/ard-work/src/main/java/com/ard/work/component/forest/ErrorInterceptor.java b/ard-work/src/main/java/com/ard/work/component/forest/ErrorInterceptor.java
new file mode 100644
index 0000000..85e43db
--- /dev/null
+++ b/ard-work/src/main/java/com/ard/work/component/forest/ErrorInterceptor.java
@@ -0,0 +1,26 @@
+package com.ard.work.component.forest;
+
+import com.dtflys.forest.exceptions.ForestRuntimeException;
+import com.dtflys.forest.http.ForestRequest;
+import com.dtflys.forest.http.ForestResponse;
+import com.dtflys.forest.interceptor.ForestInterceptor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+
+@Component
+@Slf4j
+public class ErrorInterceptor implements ForestInterceptor {
+
+
+ @Override
+ public void onError(ForestRuntimeException ex, ForestRequest request, ForestResponse response) {
+ Throwable cause = ex.getCause();
+ if (cause instanceof java.net.SocketTimeoutException) {
+ log.error("请求超时 URL:{} ", request.getUrl());
+ } else {
+ log.error("请求失败 URL:{} {}", request.getUrl(), ex.getMessage());
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/ard-work/src/main/java/com/ard/work/component/forest/TokenInterceptor.java b/ard-work/src/main/java/com/ard/work/component/forest/TokenInterceptor.java
index 2145b5e..52f29ac 100644
--- a/ard-work/src/main/java/com/ard/work/component/forest/TokenInterceptor.java
+++ b/ard-work/src/main/java/com/ard/work/component/forest/TokenInterceptor.java
@@ -2,10 +2,13 @@
import com.ard.work.domain.receiver.AccessTokenReceiver;
import com.ard.work.service.IArdServerInfoService;
+import com.dtflys.forest.exceptions.ForestRuntimeException;
import com.dtflys.forest.http.ForestRequest;
+import com.dtflys.forest.http.ForestResponse;
import com.dtflys.forest.interceptor.ForestInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
+
import javax.annotation.Resource;
@Component
@@ -23,15 +26,14 @@
public boolean beforeExecute(ForestRequest req) {
try {
String serverId = (String) req.getVariableValue("serverId");
- AccessTokenReceiver accessToken = ardServerService.getAccessToken(serverId);
- if (accessToken == null) {
- log.warn("AccessToken 为 null, serverId={}", serverId);
+ AccessTokenReceiver token = ardServerService.getAccessToken(serverId);
+ if (token == null) {
return false; // 阻止请求继续
}
- req.addHeader("x-cid", accessToken.getCompanyId());
- req.addHeader("x-token", accessToken.getAccessToken());
- } catch (Exception e) {
- log.error("TokenInterceptor 处理异常:{}", e.getMessage());
+ req.addHeader("x-cid", token.getCompanyId());
+ req.addHeader("x-token", token.getAccessToken());
+ } catch (ForestRuntimeException e) {
+ log.error("拦截器处理异常:{}", e.getMessage());
return false;
}
return true;
diff --git a/ard-work/src/main/java/com/ard/work/component/forest/TyClient.java b/ard-work/src/main/java/com/ard/work/component/forest/TyClient.java
index a5f3c23..928ea74 100644
--- a/ard-work/src/main/java/com/ard/work/component/forest/TyClient.java
+++ b/ard-work/src/main/java/com/ard/work/component/forest/TyClient.java
@@ -16,11 +16,12 @@
* @date 2024年10月21日14:45
**/
@Address(source = MyAddressSource.class)
+@BaseRequest(interceptor = ErrorInterceptor.class)
public interface TyClient {
/**
* 获取云盒TOKEN
*/
- @Post(TyConst.POST_TOKEN)
+ @Post(value=TyConst.POST_TOKEN)
TyHttpCommonReceiver<AccessTokenReceiver> getToken(@Var("serverId") String serverId, @JSONBody TokenDTO tokenDTO);
/**
diff --git a/ard-work/src/main/java/com/ard/work/service/impl/ArdDroneInfoServiceImpl.java b/ard-work/src/main/java/com/ard/work/service/impl/ArdDroneInfoServiceImpl.java
index 69639bc..4172edb 100644
--- a/ard-work/src/main/java/com/ard/work/service/impl/ArdDroneInfoServiceImpl.java
+++ b/ard-work/src/main/java/com/ard/work/service/impl/ArdDroneInfoServiceImpl.java
@@ -84,8 +84,6 @@
BoxStreamUrlReceiver boxLiveUrl = tyService.getBoxLiveUrl(uav.getBoxSn());
if (boxLiveUrl != null) {
uav.setWebrtcUrl(boxLiveUrl.getRtc());
- } else {
- log.warn("设备 {} 的直播地址为空", uav.getBoxSn());
}
} catch (Exception e) {
log.error("获取设备 {} 的直播地址失败", uav.getBoxSn(), e);
diff --git a/ard-work/src/main/java/com/ard/work/service/impl/ArdFlightTaskServiceServiceImpl.java b/ard-work/src/main/java/com/ard/work/service/impl/ArdFlightTaskServiceServiceImpl.java
index bcc4d7d..31fccef 100644
--- a/ard-work/src/main/java/com/ard/work/service/impl/ArdFlightTaskServiceServiceImpl.java
+++ b/ard-work/src/main/java/com/ard/work/service/impl/ArdFlightTaskServiceServiceImpl.java
@@ -26,13 +26,18 @@
import javax.annotation.Resource;
import javax.imageio.ImageIO;
-import javax.net.ssl.*;
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
+import java.net.URLConnection;
+import java.security.cert.X509Certificate;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@@ -165,45 +170,62 @@
// 插入视频记录
private void insertVideoToDatabase(ArdFlightTask flightTask, List<String> videoPathList) {
- if (videoPathList != null && !videoPathList.isEmpty()) {
- for (String videoPath : videoPathList) {
- ArdMedia ardMedia = ArdMedia.builder()
- .id(IdUtils.simpleUUID())
- .taskId(flightTask.getTaskId())
- .mediaType(1) // 1表示视频
- .sourceUrl(videoPath)
- .fileSize(getFileSizeByUrl(videoPath))
- .build();
- CompletableFuture.supplyAsync(() -> {
- try {
- // 上传视频
- String videoUrl = uploadFileToOss(flightTask.getBoxSn(), videoPath, "record",
- MimeTypeUtils.VIDEO_MP4);
- ardMedia.setMediaUrl(videoUrl);
- // 生成缩略图 InputStream
- ByteArrayInputStream thumbnailStream = generateThumbnailInputStream(videoPath, 2, 200);
- if (thumbnailStream != null) {
- String thumbnailUrl = ossServiceContext.upload("record",
- flightTask.getBoxSn() + "/" + IdUtils.fastSimpleUUID() + ".jpg",
- thumbnailStream,
- MimeTypeUtils.IMAGE_JPEG);
- ardMedia.setThumbnailUrl(thumbnailUrl);
+ if (videoPathList == null || videoPathList.isEmpty()) {
+ return;
+ }
+ String boxSn = flightTask.getBoxSn();
+ for (String videoPath : videoPathList) {
+
+ ArdMedia media = ArdMedia.builder()
+ .id(IdUtils.simpleUUID())
+ .taskId(flightTask.getTaskId())
+ .mediaType(1)
+ .sourceUrl(videoPath)
+ .fileSize(getFileSizeByUrl(videoPath))
+ .build();
+
+ // ---- 1. 视频上传(失败不抛异常,返回 null) ----
+ CompletableFuture<String> videoFuture = CompletableFuture.supplyAsync(() -> {
+ try (InputStream inputStream = openUrlInputStream(videoPath, 5000, 10000)) {
+ if (inputStream != null) {
+ return ossServiceContext.upload(
+ "record",
+ boxSn + "/" + media.getId() + ".mp4",
+ inputStream,
+ MimeTypeUtils.VIDEO_MP4
+ );
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return null;
+ }, threadPoolTaskExecutor);
+ // ---- 2. 缩略图上传(失败不抛异常,返回 null) ----
+ CompletableFuture<String> thumbFuture = CompletableFuture.supplyAsync(() -> {
+ try {
+ try (ByteArrayInputStream thumbStream = generateThumbnail(videoPath, 2, 200)) {
+ if (thumbStream != null) {
+ return ossServiceContext.upload(
+ "record",
+ boxSn + "/" + media.getId() + ".jpg",
+ thumbStream,
+ MimeTypeUtils.IMAGE_JPEG
+ );
}
-
- ardMedia.setCreateTime(new Date());
- return ardMedia;
-
- } catch (Exception e) {
- e.printStackTrace();
- return null;
}
- }, threadPoolTaskExecutor).thenAccept(am -> {
- if (am != null) {
- ardMediaMapper.insertArdMedia(am);
- }
- });
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return null;
+ }, threadPoolTaskExecutor);
- }
+ // 两个完成后写数据库(任意失败不阻断流程)
+ videoFuture.thenCombine(thumbFuture, (vUrl, tUrl) -> {
+ media.setMediaUrl(vUrl);
+ media.setThumbnailUrl(tUrl);
+ media.setCreateTime(new Date());
+ return media;
+ }).thenAcceptAsync(ardMediaMapper::insertArdMedia, threadPoolTaskExecutor);
}
}
@@ -214,7 +236,7 @@
Integer count = flightTask.getVideoCount();//获取本地库视频数量
int size = videoPathList.size();//获取接口返回的视频数量
if (count != null && size > count) {
- log.info("录像数量不一致,需要更新数据库");
+ log.info("任务【{}】录像数量不一致,需要更新数据库", flightTask.getTaskId());
// 需要插入新的视频
List<ArdMedia> existingVideos = ardMediaMapper.selectArdMediaByTaskIdAndType(flightTask.getTaskId(), 1);
// 提取现有的视频路径
@@ -227,6 +249,7 @@
.filter(videoPath -> !existingVideoPaths.contains(videoPath)) // 过滤出新的视频
.collect(Collectors.toList());
if (!newVideos.isEmpty()) {
+ String boxSn = flightTask.getBoxSn();
// 插入新的视频记录
for (String videoPath : newVideos) {
ArdMedia ardMedia = ArdMedia.builder()
@@ -236,36 +259,47 @@
.fileSize(getFileSizeByUrl(videoPath))
.sourceUrl(videoPath)
.build();
- CompletableFuture.supplyAsync(() -> {
- try {
- // 上传视频
- String videoUrl = uploadFileToOss(flightTask.getBoxSn(), videoPath, "record",
- MimeTypeUtils.VIDEO_MP4);
- ardMedia.setMediaUrl(videoUrl);
-
-
- // 生成缩略图 InputStream
- ByteArrayInputStream thumbnailStream = generateThumbnailInputStream(videoPath, 2, 200);
- if (thumbnailStream != null) {
- String thumbnailUrl = ossServiceContext.upload("record",
- flightTask.getBoxSn() + "/" + IdUtils.fastSimpleUUID() + ".jpg",
- thumbnailStream,
- MimeTypeUtils.IMAGE_JPEG);
- ardMedia.setThumbnailUrl(thumbnailUrl);
+ // ---- 1. 视频上传(失败不抛异常,返回 null) ----
+ CompletableFuture<String> videoFuture = CompletableFuture.supplyAsync(() -> {
+ try (InputStream inputStream = openUrlInputStream(videoPath, 5000, 10000)) {
+ if (inputStream != null) {
+ return ossServiceContext.upload(
+ "record",
+ boxSn + "/" + ardMedia.getId() + ".mp4",
+ inputStream,
+ MimeTypeUtils.VIDEO_MP4
+ );
}
-
- ardMedia.setCreateTime(new Date());
- return ardMedia;
-
} catch (Exception e) {
e.printStackTrace();
- return null;
}
- }, threadPoolTaskExecutor).thenAccept(am -> {
- if (am != null) {
- ardMediaMapper.insertArdMedia(am);
+ return null;
+ }, threadPoolTaskExecutor);
+ // ---- 2. 缩略图上传(失败不抛异常,返回 null) ----
+ CompletableFuture<String> thumbFuture = CompletableFuture.supplyAsync(() -> {
+ try {
+ try (ByteArrayInputStream thumbStream = generateThumbnail(videoPath, 2, 200)) {
+ if (thumbStream != null) {
+ return ossServiceContext.upload(
+ "record",
+ boxSn + "/" + ardMedia.getId() + ".jpg",
+ thumbStream,
+ MimeTypeUtils.IMAGE_JPEG
+ );
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
}
- });
+ return null;
+ }, threadPoolTaskExecutor);
+ // 两个完成后写数据库(任意失败不阻断流程)
+ videoFuture.thenCombine(thumbFuture, (vUrl, tUrl) -> {
+ ardMedia.setMediaUrl(vUrl);
+ ardMedia.setThumbnailUrl(tUrl);
+ ardMedia.setCreateTime(new Date());
+ return ardMedia;
+ }).thenAcceptAsync(ardMediaMapper::insertArdMedia, threadPoolTaskExecutor);
}
// 更新任务的视频数量
flightTask.setVideoCount(flightTask.getVideoCount() + newVideos.size());
@@ -302,7 +336,7 @@
int size = photoList.size();
if (size > count) {
- log.info("图片数量不一致,需要更新数据库");
+ log.info("任务【{}】图片数量不一致,需要更新数据库", flightTask.getTaskId());
// 获取本地数据库中已存在的图片记录(类型为 0 表示图片)
List<ArdMedia> existingPhotos = ardMediaMapper.selectArdMediaByTaskIdAndType(flightTask.getTaskId(), 0);
// 需要插入新的视频
@@ -327,6 +361,7 @@
}
private void addPhotos(ArdFlightTask flightTask, List<BoxPhotosReceiver> photoList) {
+ String boxSn = flightTask.getBoxSn();
for (BoxPhotosReceiver photo : photoList) {
ArdMedia ardMedia = ArdMedia.builder()
.id(IdUtils.simpleUUID())
@@ -342,78 +377,29 @@
.captureTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, photo.getTimeStamp()))
.build();
// 异步上传文件到OSS,并在上传完成后插入数据库
- CompletableFuture.supplyAsync(() -> uploadFileToOss(flightTask.getBoxSn(), photo.getImgUrl(), "pic",
- MimeTypeUtils.IMAGE_JPG), threadPoolTaskExecutor)
+ CompletableFuture.supplyAsync(() -> {
+ try (InputStream inputStream = openUrlInputStream(photo.getImgUrl(), 5000, 10000)) {
+ if (inputStream != null) {
+ return ossServiceContext.upload(
+ "pic",
+ boxSn + "/" + ardMedia.getId() + ".jpg",
+ inputStream,
+ MimeTypeUtils.IMAGE_JPG
+ );
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return null;
+ }, threadPoolTaskExecutor)
.thenAccept(url -> {
- ardMedia.setMediaUrl(url); // 获取上传后的文件 URL
+ ardMedia.setMediaUrl(url);
ardMedia.setCreateTime(new Date());
- ardMediaMapper.insertArdMedia(ardMedia); // 插入数据库
+ ardMediaMapper.insertArdMedia(ardMedia);
});
}
}
-
- // 通用的上传方法,支持视频和图片等文件上传
- private String uploadFileToOss(String boxSn, String fileUrl, String bucket, String contentType) {
- InputStream fileInputStream = null;
- String fileOssUrl = "";
- try {
- // 创建信任所有证书的SSLContext
- TrustManager[] trustAllCerts = new TrustManager[]{
- new X509TrustManager() {
- @Override
- public java.security.cert.X509Certificate[] getAcceptedIssuers() {
- return null;
- }
-
- @Override
- public void checkClientTrusted(
- java.security.cert.X509Certificate[] certs, String authType) {
- }
-
- @Override
- public void checkServerTrusted(
- java.security.cert.X509Certificate[] certs, String authType) {
- }
- }
- };
-
- SSLContext sc = SSLContext.getInstance("SSL");
- sc.init(null, trustAllCerts, new java.security.SecureRandom());
- HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
-
- // 创建所有主机名都信任的HostnameVerifier
- HostnameVerifier allHostsValid = (hostname, session) -> true;
- HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
-
- // 现在可以正常访问
-
- // 从URL创建InputStream
- URL url = new URL(fileUrl);
- fileInputStream = url.openStream();
-
- // 文件名
- String fileName = boxSn + "/" + IdUtils.fastSimpleUUID() + "." + MimeTypeUtils.getExtension(contentType);
-
- // 调用OSS服务上下文进行文件上传
- fileOssUrl = ossServiceContext.upload(bucket, fileName, fileInputStream, contentType);
-
- } catch (Exception e) {
- e.printStackTrace();
- // 异常处理,可以选择记录日志或者抛出业务异常
- } finally {
- // 关闭InputStream
- if (fileInputStream != null) {
- try {
- fileInputStream.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
-
- return fileOssUrl; // 返回上传后的文件OSS URL
- }
// 获取文件大小
private long getFileSizeByUrl(String fileUrl) {
@@ -429,9 +415,7 @@
}
// 生成视频缩略图
- public ByteArrayInputStream generateThumbnailInputStream(String videoFile,
- int skipFrames,
- int targetWidth) throws Exception {
+ public ByteArrayInputStream generateThumbnail(String videoFile, int skipFrames, int targetWidth) {
long start = System.currentTimeMillis();
FFmpegFrameGrabber grabber = null;
@@ -480,6 +464,8 @@
return new ByteArrayInputStream(baos.toByteArray());
+ } catch (Exception e) {
+ throw new RuntimeException(e);
} finally {
if (grabber != null) {
try {
@@ -492,5 +478,52 @@
}
}
+ /**
+ * 将 URL 转成 InputStream
+ *
+ * @param fileUrl 文件 URL
+ * @param connectTimeoutMillis 连接超时(毫秒)
+ * @param readTimeoutMillis 读取超时(毫秒)
+ * @return InputStream,如果失败返回 null
+ */
+ public InputStream openUrlInputStream(String fileUrl, int connectTimeoutMillis, int readTimeoutMillis) {
+ try {
+ URL url = new URL(fileUrl);
+
+ // 支持 HTTPS 信任所有证书(如果需要访问自签名证书)
+ if ("https".equalsIgnoreCase(url.getProtocol())) {
+ TrustManager[] trustAllCerts = new TrustManager[]{
+ new X509TrustManager() {
+ @Override
+ public void checkClientTrusted(X509Certificate[] chain, String authType) {
+ }
+
+ @Override
+ public void checkServerTrusted(X509Certificate[] chain, String authType) {
+ }
+
+ @Override
+ public X509Certificate[] getAcceptedIssuers() {
+ return null;
+ }
+ }
+ };
+ SSLContext sc = SSLContext.getInstance("SSL");
+ sc.init(null, trustAllCerts, new java.security.SecureRandom());
+ HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
+ HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
+ }
+
+ URLConnection connection = url.openConnection();
+ connection.setConnectTimeout(connectTimeoutMillis);
+ connection.setReadTimeout(readTimeoutMillis);
+
+ return connection.getInputStream();
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
}
diff --git a/ard-work/src/main/java/com/ard/work/service/impl/ArdServerInfoServiceImpl.java b/ard-work/src/main/java/com/ard/work/service/impl/ArdServerInfoServiceImpl.java
index 351b7ab..dc38ada 100644
--- a/ard-work/src/main/java/com/ard/work/service/impl/ArdServerInfoServiceImpl.java
+++ b/ard-work/src/main/java/com/ard/work/service/impl/ArdServerInfoServiceImpl.java
@@ -10,6 +10,7 @@
import com.ard.work.domain.receiver.TyHttpCommonReceiver;
import com.ard.work.mapper.ArdServerInfoMapper;
import com.ard.work.service.IArdServerInfoService;
+import com.dtflys.forest.exceptions.ForestRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
@@ -124,7 +125,7 @@
@Override
public AccessTokenReceiver getAccessToken(String id) {
long timeStamp = getTimeStamp();
- AccessTokenReceiver accessToken = null;
+
ArdServerInfo serverInfo = selectArdServerInfoById(id);
// 准备请求的json
TokenDTO gtd = TokenDTO.builder()
@@ -132,22 +133,22 @@
.encryptStr(getEncryptStr(serverInfo, timeStamp))
.timeStamp(timeStamp)
.build();
- try {
+ // try {
TyHttpCommonReceiver<AccessTokenReceiver> getTokenRes = tyClient.getToken(id, gtd);
- accessToken = getTokenRes.getContent();
- } catch (Exception e) {
- log.error("获取token失败:{}", e.getMessage());
- }
+ return getTokenRes.getContent();
+// } catch (Exception e) {
+// log.error("获取 Token 失败 serverId={}, 异常: {}", id, e.getMessage());
+// return null; // 请求失败返回 null
+// }
//{"state":0,"content":{"companyId":4127,"accessToken":"sOONT0MMVdoKcMBibb40xxgrUrq0Hw9RFiajic3KOzoNtk"}}
- return accessToken;
}
/**
* 修改服务器状态
*/
@Override
- public int updateServerStatus(String id,String status) {
- return ardServerInfoMapper.updateServerStatus(id,status);
+ public int updateServerStatus(String id, String status) {
+ return ardServerInfoMapper.updateServerStatus(id, status);
}
@Override
--
Gitblit v1.9.3