fix: 附件上传加日志,并且优化上传地址问题

This commit is contained in:
tangwei 2026-05-08 09:24:55 +08:00
parent 3a944aaf2a
commit 03f2e1c669
6 changed files with 90 additions and 26 deletions

View File

@ -137,13 +137,16 @@ public class FishDraftDataController {
ImportTask importTask = importTaskService.getById(taskId);
String resultJson = importTask.getResultJson();
FishImportResult importResult = null;
log.info("==============批量保存草稿================: {}", taskId);
if (resultJson != null && !resultJson.isEmpty()) {
try {
importResult = objectMapper.readValue(resultJson, FishImportResult.class);
ZipFileUtil.ZipContent content = new ZipFileUtil.ZipContent();
content.images = importResult.getImageFiles();
content.videos = importResult.getVideoFiles();
log.info("==============processAttachments前================: {}", taskId);
fishImportService.processAttachments(importResult, content);
log.info("==============processAttachments后================: {}", taskId);
} catch (Exception e) {
e.printStackTrace();
// ignore parse error

View File

@ -32,13 +32,16 @@ public class AttachmentUploadService {
@Value("${attachment.token}")
private String token;
// 定义一个固定的线程池用于文件上传建议根据服务器性能调整核心线程数
@Value("${attachment.upload-url}")
private String uploadUrl;
@Value("${attachment.video-url}")
private String videoUrl;
private static final ExecutorService UPLOAD_EXECUTOR = new ThreadPoolExecutor(
5, 10, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(100),
new ThreadPoolExecutor.CallerRunsPolicy()
);
private static final String UPLOAD_URL = "https://211.99.26.225:12125/upload";
private static final String VIDEO_URL = "https://211.99.26.225:12125/upload";
private final HttpClient httpClient;
@ -68,32 +71,14 @@ public class AttachmentUploadService {
System.arraycopy(footer.getBytes(), 0, body, header.getBytes().length + fileContent.length, footer.getBytes().length);
try {
// 1. 创建信任所有证书的 TrustManager
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
}
};
// 2. 初始化 SSLContext
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new SecureRandom());
// 3. 构建支持 HTTPS 且忽略证书验证的 HttpClient
HttpClient secureClient = HttpClient.newBuilder()
.sslContext(sc)
.build();
log.info("开始上传文件:uploadUrl {}", uploadUrl);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(UPLOAD_URL))
.uri(URI.create(uploadUrl))
.header("Content-Type", "multipart/form-data; boundary=" + boundary)
.POST(HttpRequest.BodyPublishers.ofByteArray(body))
.build();
// 4. 使用新的 secureClient 发送请求
HttpResponse<String> response = secureClient.send(request, HttpResponse.BodyHandlers.ofString());
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
String responseBody = response.body();
@ -109,6 +94,68 @@ public class AttachmentUploadService {
}
// public String uploadFile(File file) throws IOException, InterruptedException {
// if (file == null || !file.exists()) {
// log.warn("文件不存在或为空: {}", file);
// return null;
// }
//
// String boundary = "----FormBoundary" + System.currentTimeMillis();
// byte[] fileContent = Files.readAllBytes(file.toPath());
// String fileName = file.getName();
//
// String header = "--" + boundary + "\r\n" +
// "Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName + "\"\r\n" +
// "Content-Type: application/octet-stream\r\n\r\n";
// String footer = "\r\n--" + boundary + "--\r\n";
//
// byte[] body = new byte[header.getBytes().length + fileContent.length + footer.getBytes().length];
// System.arraycopy(header.getBytes(), 0, body, 0, header.getBytes().length);
// System.arraycopy(fileContent, 0, body, header.getBytes().length, fileContent.length);
// System.arraycopy(footer.getBytes(), 0, body, header.getBytes().length + fileContent.length, footer.getBytes().length);
//
// try {
// // 1. 创建信任所有证书的 TrustManager
// TrustManager[] trustAllCerts = new TrustManager[]{
// new X509TrustManager() {
// public void checkClientTrusted(X509Certificate[] chain, String authType) {}
// public void checkServerTrusted(X509Certificate[] chain, String authType) {}
// public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
// }
// };
//
// // 2. 初始化 SSLContext
// SSLContext sc = SSLContext.getInstance("TLS");
// sc.init(null, trustAllCerts, new SecureRandom());
//
// // 3. 构建支持 HTTPS 且忽略证书验证的 HttpClient
// HttpClient secureClient = HttpClient.newBuilder()
// .sslContext(sc)
// .build();
//
// HttpRequest request = HttpRequest.newBuilder()
// .uri(URI.create(uploadUrl))
// .header("Content-Type", "multipart/form-data; boundary=" + boundary)
// .POST(HttpRequest.BodyPublishers.ofByteArray(body))
// .build();
//
// // 4. 使用新的 secureClient 发送请求
// HttpResponse<String> response = secureClient.send(request, HttpResponse.BodyHandlers.ofString());
//
// if (response.statusCode() == 200) {
// String responseBody = response.body();
// return parseAttachmentId(responseBody);
// } else {
// log.error("上传文件失败: {}, 状态码: {}", fileName, response.statusCode());
// return null;
// }
// } catch (Exception e) {
// log.error("文件上传过程中发生异常: {}", e.getMessage(), e);
// return null;
// }
// }
/**
* 多线程批量上传文件
* @param files 文件列表

View File

@ -1301,7 +1301,9 @@ public class FishImportServiceImpl implements IFishImportService {
fileMap.put("name", fileName);
if (actualPath != null) {
try {
log.info("开始上传视频文件: {}" ,actualPath);
String attachmentId = attachmentUploadService.uploadFileAndGetId(actualPath);
log.info("开始上传视频文件后:{}attachmentId{} " ,actualPath,attachmentId);
if (attachmentId != null) {
attachmentIds.add(attachmentId);
fileMap.put("value", attachmentId);
@ -1341,7 +1343,9 @@ public class FishImportServiceImpl implements IFishImportService {
String actualPath = findFilePath(fileName, imageMap);
if (actualPath != null) {
try {
log.info("开始上传图片文件: {}" ,actualPath);
String attachmentId = attachmentUploadService.uploadFileAndGetId(actualPath);
log.info("开始上传图片文件后:{}attachmentId{} " ,actualPath,attachmentId);
if (attachmentId != null) {
attachmentIds.add(attachmentId);
fileMap.put("value", attachmentId);

View File

@ -117,3 +117,10 @@ task:
keep-alive-seconds: 60
# 队列容量
queue-capacity: 50
attachment:
token: ${ATTACHMENT_TOKEN:qgcBkod25ngBa4wu8BtfCPYsJ7lQGVDoexH}
upload-url: ${ATTACHMENT_UPLOAD_URL:http://172.16.31.185:18200/upload}
video-url: ${ATTACHMENT_VIDEO_URL:http://172.16.31.185:18200/upload}
# upload-url: ${ATTACHMENT_UPLOAD_URL:https://211.99.26.225:12125/upload}
# video-url: ${ATTACHMENT_VIDEO_URL:https://211.99.26.225:12125/upload}

View File

@ -117,3 +117,8 @@ task:
keep-alive-seconds: 60
# 队列容量
queue-capacity: 50
attachment:
token: ${ATTACHMENT_TOKEN:qgcBkod25ngBa4wu8BtfCPYsJ7lQGVDoexH}
upload-url: ${ATTACHMENT_UPLOAD_URL:http://172.16.31.185:18200/upload}
video-url: ${ATTACHMENT_VIDEO_URL:http://172.16.31.185:18200/upload}

View File

@ -40,5 +40,3 @@ springdoc:
path: /swagger-ui.html
packages-to-scan: com.yfd.platform
attachment:
token: ${ATTACHMENT_TOKEN:qgcBkod25ngBa4wu8BtfCPYsJ7lQGVDoexH}