|
|
|
@ -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
|
|
|
|
|
return new MockMultipartFile( |
|
|
|
|
"file", // 表单字段名,这通常与你的Spring MVC控制器中的@RequestParam("file")匹配
|
|
|
|
|
fileName, // 文件名
|
|
|
|
|
"image/jpeg", // MIME类型
|
|
|
|
|
fileBytes // 文件内容字节数组
|
|
|
|
|
); |
|
|
|
|
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类型
|
|
|
|
|
EntityUtils.toByteArray(entity) // 文件内容字节数组
|
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
throw new HzServiceException("获取文件失败!"); |
|
|
|
|
} catch(Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
throw new HzServiceException("获取文件失败!"); |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|