Browse Source

fix: 修复海康视频截图链接协议为https时图片上传minio报错问题

zhongwei
haungxing 2 months ago
parent
commit
f6811b02cc
  1. 7
      hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/task/service/impl/FileUploaderService.java
  2. 41
      hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/utils/FileUtil.java

7
hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/task/service/impl/FileUploaderService.java

@ -1,5 +1,6 @@
package com.hnac.hzinfo.inspect.task.service.impl;
import com.hnac.hzims.common.constant.CommonConstant;
import com.hnac.hzinfo.exception.HzServiceException;
import com.hnac.hzinfo.inspect.utils.FileUtil;
import lombok.AllArgsConstructor;
@ -31,7 +32,7 @@ public class FileUploaderService {
*/
public BladeFile upload(String url,String fileName) {
MockMultipartFile file = FileUtil.urlToMultipartFile(url, fileName);
return this.upload(file);
return this.upload(file, fileName);
}
/**
@ -39,8 +40,8 @@ public class FileUploaderService {
* @param file 待上传文件
* @return 上传结果
*/
public BladeFile upload(MockMultipartFile file) {
R<BladeFile> result = ossClient.putFile(file);
public BladeFile upload(MockMultipartFile file,String fileName) {
R<BladeFile> result = ossClient.putFileByNameAndTenantId(fileName, file, CommonConstant.TENANT_ID);
Assert.isTrue(result.isSuccess(), () -> {
throw new HzServiceException(ResultCode.FAILURE,"文件上传失败!");
});

41
hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/utils/FileUtil.java

@ -3,6 +3,13 @@ package com.hnac.hzinfo.inspect.utils;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.hnac.hzinfo.exception.HzServiceException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.tool.api.IResultCode;
import org.springblade.core.tool.api.R;
@ -14,8 +21,15 @@ import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import javax.net.ssl.SSLContext;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import org.apache.http.ssl.SSLContextBuilder;
/**
* @Author: huangxing
@ -24,19 +38,32 @@ import java.io.InputStream;
public class FileUtil {
public static MockMultipartFile urlToMultipartFile(String url, String fileName) {
HttpResponse response = HttpRequest.get(url).execute();
if (response.isOk()) {
// 读取响应体为字节数组
byte[] fileBytes = response.bodyBytes();
// 使用字节数组、文件名和MIME类型创建MockMultipartFile
try {
// 创建一个不验证主机名且接受任何证书的SSLContext
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (x509Certificates, s) -> true).build();
// 使用该SSLContext创建一个HttpClient,并禁用主机名验证
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLContext(sslContext)
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build();
HttpGet request = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == 200) {
org.apache.http.HttpEntity entity = response.getEntity();
if (entity != null) {
return new MockMultipartFile(
"file", // 表单字段名,这通常与你的Spring MVC控制器中的@RequestParam("file")匹配
fileName, // 文件名
"image/jpeg", // MIME类型
fileBytes // 文件内容字节数组
EntityUtils.toByteArray(entity) // 文件内容字节数组
);
}
return null;
}
throw new HzServiceException("获取文件失败!");
} catch(Exception e) {
e.printStackTrace();
throw new HzServiceException("获取文件失败!");
}
}
}

Loading…
Cancel
Save