fix: 附件上传加日志,并且优化上传地址问题
This commit is contained in:
parent
3a944aaf2a
commit
03f2e1c669
@ -137,13 +137,16 @@ public class FishDraftDataController {
|
|||||||
ImportTask importTask = importTaskService.getById(taskId);
|
ImportTask importTask = importTaskService.getById(taskId);
|
||||||
String resultJson = importTask.getResultJson();
|
String resultJson = importTask.getResultJson();
|
||||||
FishImportResult importResult = null;
|
FishImportResult importResult = null;
|
||||||
|
log.info("==============批量保存草稿================: {}", taskId);
|
||||||
if (resultJson != null && !resultJson.isEmpty()) {
|
if (resultJson != null && !resultJson.isEmpty()) {
|
||||||
try {
|
try {
|
||||||
importResult = objectMapper.readValue(resultJson, FishImportResult.class);
|
importResult = objectMapper.readValue(resultJson, FishImportResult.class);
|
||||||
ZipFileUtil.ZipContent content = new ZipFileUtil.ZipContent();
|
ZipFileUtil.ZipContent content = new ZipFileUtil.ZipContent();
|
||||||
content.images = importResult.getImageFiles();
|
content.images = importResult.getImageFiles();
|
||||||
content.videos = importResult.getVideoFiles();
|
content.videos = importResult.getVideoFiles();
|
||||||
|
log.info("==============processAttachments前================: {}", taskId);
|
||||||
fishImportService.processAttachments(importResult, content);
|
fishImportService.processAttachments(importResult, content);
|
||||||
|
log.info("==============processAttachments后================: {}", taskId);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
// ignore parse error
|
// ignore parse error
|
||||||
|
|||||||
@ -32,13 +32,16 @@ public class AttachmentUploadService {
|
|||||||
@Value("${attachment.token}")
|
@Value("${attachment.token}")
|
||||||
private String 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(
|
private static final ExecutorService UPLOAD_EXECUTOR = new ThreadPoolExecutor(
|
||||||
5, 10, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(100),
|
5, 10, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(100),
|
||||||
new ThreadPoolExecutor.CallerRunsPolicy()
|
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;
|
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);
|
System.arraycopy(footer.getBytes(), 0, body, header.getBytes().length + fileContent.length, footer.getBytes().length);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 1. 创建信任所有证书的 TrustManager
|
log.info("开始上传文件:uploadUrl {}", uploadUrl);
|
||||||
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()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(URI.create(UPLOAD_URL))
|
.uri(URI.create(uploadUrl))
|
||||||
.header("Content-Type", "multipart/form-data; boundary=" + boundary)
|
.header("Content-Type", "multipart/form-data; boundary=" + boundary)
|
||||||
.POST(HttpRequest.BodyPublishers.ofByteArray(body))
|
.POST(HttpRequest.BodyPublishers.ofByteArray(body))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// 4. 使用新的 secureClient 发送请求
|
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
HttpResponse<String> response = secureClient.send(request, HttpResponse.BodyHandlers.ofString());
|
|
||||||
|
|
||||||
if (response.statusCode() == 200) {
|
if (response.statusCode() == 200) {
|
||||||
String responseBody = response.body();
|
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 文件列表
|
* @param files 文件列表
|
||||||
|
|||||||
@ -1301,7 +1301,9 @@ public class FishImportServiceImpl implements IFishImportService {
|
|||||||
fileMap.put("name", fileName);
|
fileMap.put("name", fileName);
|
||||||
if (actualPath != null) {
|
if (actualPath != null) {
|
||||||
try {
|
try {
|
||||||
|
log.info("开始上传视频文件: {}" ,actualPath);
|
||||||
String attachmentId = attachmentUploadService.uploadFileAndGetId(actualPath);
|
String attachmentId = attachmentUploadService.uploadFileAndGetId(actualPath);
|
||||||
|
log.info("开始上传视频文件后:{}attachmentId{} " ,actualPath,attachmentId);
|
||||||
if (attachmentId != null) {
|
if (attachmentId != null) {
|
||||||
attachmentIds.add(attachmentId);
|
attachmentIds.add(attachmentId);
|
||||||
fileMap.put("value", attachmentId);
|
fileMap.put("value", attachmentId);
|
||||||
@ -1341,7 +1343,9 @@ public class FishImportServiceImpl implements IFishImportService {
|
|||||||
String actualPath = findFilePath(fileName, imageMap);
|
String actualPath = findFilePath(fileName, imageMap);
|
||||||
if (actualPath != null) {
|
if (actualPath != null) {
|
||||||
try {
|
try {
|
||||||
|
log.info("开始上传图片文件: {}" ,actualPath);
|
||||||
String attachmentId = attachmentUploadService.uploadFileAndGetId(actualPath);
|
String attachmentId = attachmentUploadService.uploadFileAndGetId(actualPath);
|
||||||
|
log.info("开始上传图片文件后:{}attachmentId{} " ,actualPath,attachmentId);
|
||||||
if (attachmentId != null) {
|
if (attachmentId != null) {
|
||||||
attachmentIds.add(attachmentId);
|
attachmentIds.add(attachmentId);
|
||||||
fileMap.put("value", attachmentId);
|
fileMap.put("value", attachmentId);
|
||||||
|
|||||||
@ -117,3 +117,10 @@ task:
|
|||||||
keep-alive-seconds: 60
|
keep-alive-seconds: 60
|
||||||
# 队列容量
|
# 队列容量
|
||||||
queue-capacity: 50
|
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}
|
||||||
@ -117,3 +117,8 @@ task:
|
|||||||
keep-alive-seconds: 60
|
keep-alive-seconds: 60
|
||||||
# 队列容量
|
# 队列容量
|
||||||
queue-capacity: 50
|
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}
|
||||||
@ -40,5 +40,3 @@ springdoc:
|
|||||||
path: /swagger-ui.html
|
path: /swagger-ui.html
|
||||||
packages-to-scan: com.yfd.platform
|
packages-to-scan: com.yfd.platform
|
||||||
|
|
||||||
attachment:
|
|
||||||
token: ${ATTACHMENT_TOKEN:qgcBkod25ngBa4wu8BtfCPYsJ7lQGVDoexH}
|
|
||||||
Loading…
Reference in New Issue
Block a user