diff --git a/de-xpack b/de-xpack index 867808ba4b..d2f7d2fa48 160000 --- a/de-xpack +++ b/de-xpack @@ -1 +1 @@ -Subproject commit 867808ba4bb0509ad2e05d9f9b1228c2d5ff44c4 +Subproject commit d2f7d2fa4834fd5eaa8db0401c5fc195c15cf37e diff --git a/sdk/common/src/main/java/io/dataease/utils/HttpClientUtil.java b/sdk/common/src/main/java/io/dataease/utils/HttpClientUtil.java index 7c7a74cc83..a8ef27cc28 100755 --- a/sdk/common/src/main/java/io/dataease/utils/HttpClientUtil.java +++ b/sdk/common/src/main/java/io/dataease/utils/HttpClientUtil.java @@ -18,6 +18,7 @@ import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; @@ -29,6 +30,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.security.cert.X509Certificate; @@ -438,6 +440,50 @@ public class HttpClientUtil { return HttpClientUtil.postFile(url, bytes, name, paramMap, config); } + public static String upload(String url, File file, String name) { + HttpClientConfig config = new HttpClientConfig(); + Map param = new HashMap<>(); + param.put("fileFlag", "media"); + param.put("fileName", name); + return HttpClientUtil.postFile(url, file, param, config); + } + + public static String postFile(String fileServer, File file, Map param, HttpClientConfig config) { + CloseableHttpClient httpClient = buildHttpClient(fileServer); + HttpPost postRequest = new HttpPost(fileServer); + if (config == null) { + config = new HttpClientConfig(); + } + postRequest.setConfig(config.buildRequestConfig()); + Map header = config.getHeader(); + String fileFlag = param.get("fileFlag"); + String fileName = param.get("fileName"); + param.remove("fileFlag"); + param.remove("fileName"); + if (MapUtils.isNotEmpty(header)) { + for (String key : header.keySet()) { + postRequest.addHeader(key, header.get(key)); + } + } + postRequest.setHeader("Content-Type", "multipart/form-data"); + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setCharset(StandardCharsets.UTF_8); + builder.addBinaryBody(StringUtils.isNotBlank(fileFlag) ? fileFlag : "file", file, ContentType.APPLICATION_OCTET_STREAM, StringUtils.isNotBlank(fileName) ? fileName : file.getName()); + if (MapUtils.isNotEmpty(param)) { + for (Map.Entry entry : param.entrySet()) { + StringBody stringBody = new StringBody(entry.getValue(), ContentType.TEXT_PLAIN.withCharset("utf-8")); + builder.addPart(entry.getKey(), stringBody); + } + } + try { + postRequest.setEntity(builder.build()); + return getResponseStr(httpClient.execute(postRequest), config); + } catch (Exception e) { + logger.error("HttpClient查询失败", e); + throw new RuntimeException("HttpClient查询失败: " + e.getMessage()); + } + } + private static void addHead(HttpClientConfig config, Map headMap) { if (MapUtils.isEmpty(headMap)) return; for (Map.Entry entry : headMap.entrySet()) {