|
@@ -1,20 +1,9 @@
|
|
|
package com.sckw.ai.biz.util;
|
|
package com.sckw.ai.biz.util;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
-import com.sckw.core.utils.UUIDUtils;
|
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import okhttp3.*;
|
|
import okhttp3.*;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
-import org.apache.hc.client5.http.classic.methods.HttpPost;
|
|
|
|
|
-import org.apache.hc.client5.http.entity.mime.FileBody;
|
|
|
|
|
-import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
|
|
|
|
|
-import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
|
|
|
|
|
-import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
|
|
|
|
-import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
|
|
|
|
|
-import org.apache.hc.client5.http.impl.classic.HttpClients;
|
|
|
|
|
-import org.apache.hc.core5.http.ContentType;
|
|
|
|
|
-import org.apache.hc.core5.http.HttpEntity;
|
|
|
|
|
-import org.apache.hc.core5.http.io.entity.EntityUtils;
|
|
|
|
|
import org.apache.http.client.methods.HttpRequestBase;
|
|
import org.apache.http.client.methods.HttpRequestBase;
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
import org.springframework.util.CollectionUtils;
|
|
@@ -23,7 +12,6 @@ import javax.net.ssl.SSLContext;
|
|
|
import javax.net.ssl.SSLSocketFactory;
|
|
import javax.net.ssl.SSLSocketFactory;
|
|
|
import javax.net.ssl.TrustManager;
|
|
import javax.net.ssl.TrustManager;
|
|
|
import javax.net.ssl.X509TrustManager;
|
|
import javax.net.ssl.X509TrustManager;
|
|
|
-import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
@@ -182,25 +170,7 @@ public class OkHttpUtils {
|
|
|
request = new Request.Builder().post(requestBody).url(buildUrl());
|
|
request = new Request.Builder().post(requestBody).url(buildUrl());
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
|
- /**
|
|
|
|
|
- * 初始化post方法
|
|
|
|
|
- */
|
|
|
|
|
- public OkHttpUtils postForm(JSONObject object, byte[] file) {
|
|
|
|
|
- MultipartBody.Builder formBody = new MultipartBody.Builder();
|
|
|
|
|
- formBody.setType(MultipartBody.FORM);
|
|
|
|
|
-
|
|
|
|
|
- for (String s : object.keySet()) {
|
|
|
|
|
- formBody.addFormDataPart(s, StringUtils.isBlank(object.getString(s)) ? "" : object.getString(s));
|
|
|
|
|
- }
|
|
|
|
|
- String fileName = UUIDUtils.get32UUID() + ".zip";
|
|
|
|
|
- log.info("文件名:{}", fileName);
|
|
|
|
|
- formBody.addFormDataPart("file", fileName, RequestBody.create(file));
|
|
|
|
|
|
|
|
|
|
- RequestBody requestBody = formBody.build();
|
|
|
|
|
- // params参数
|
|
|
|
|
- request = new Request.Builder().post(requestBody).url(buildUrl());
|
|
|
|
|
- return this;
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 初始化post方法
|
|
* 初始化post方法
|
|
@@ -404,86 +374,9 @@ public class OkHttpUtils {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 使用示例
|
|
|
|
|
- *
|
|
|
|
|
- * @param args
|
|
|
|
|
- * @author xcq
|
|
|
|
|
- * @date 2023-02-21 10:47:28
|
|
|
|
|
- **/
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
|
|
- String address = "https://file.cloudpnr.com/app-86820a0f-8b13-479b-a466-b261af7290d5%2Fsaturnfile%2F48cf909816b142e5beec68988c1982b1%2F39189ab6-fac3-11ed-9690-0242ac110002.zip?Expires=1685254390&OSSAccessKeyId=LTAI6Yzq9tIYS57h&Signature=oNyqey777CjDk0IH5ZaniwTAdfg%3D";
|
|
|
|
|
- byte[] file = OkHttpUtils.builder().url(address)
|
|
|
|
|
- .get()
|
|
|
|
|
- .fileSync();
|
|
|
|
|
- /*通知中台*/
|
|
|
|
|
- JSONObject tmp = new JSONObject();
|
|
|
|
|
- tmp.put("status", StringUtils.equals("00000000", "00000000") ? "true" : "false");
|
|
|
|
|
- tmp.put("msg", "");
|
|
|
|
|
- log.info("交易确认异步通知中台入参:{}", tmp.toJSONString());
|
|
|
|
|
-
|
|
|
|
|
- String sync = OkHttpUtils.builder().url("http://10.10.10.241:9505/notice/huifu/signal_agent_pay/S520267896211968001/00dd5d5dd4682ea3fad88a37ab48223a")
|
|
|
|
|
- .postForm(tmp, file)
|
|
|
|
|
- .sync();
|
|
|
|
|
- log.info("交易确认异步通知中台返回->{}", sync);
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * post请求提交form-data上传文件
|
|
|
|
|
- *
|
|
|
|
|
- * @param url
|
|
|
|
|
- * @return
|
|
|
|
|
- */
|
|
|
|
|
- public static String doPostUploadFile(String url, Map<String, Object> param) {
|
|
|
|
|
- HttpPost httpPost = new HttpPost(url);
|
|
|
|
|
- CloseableHttpResponse response = null;
|
|
|
|
|
- String respContent = null;
|
|
|
|
|
- CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
|
|
- try {
|
|
|
|
|
- String boundary = "--" + UUIDUtils.get32UUID();
|
|
|
|
|
- httpPost.setHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
|
|
|
|
|
- MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.LEGACY);
|
|
|
|
|
- builder.setCharset(StandardCharsets.UTF_8);
|
|
|
|
|
- builder.setBoundary(boundary);
|
|
|
|
|
- for (String key : param.keySet()) {
|
|
|
|
|
- Object value = param.get(key);
|
|
|
|
|
- if (value instanceof File file) {
|
|
|
|
|
- FileBody fileBody = new FileBody(file);
|
|
|
|
|
- builder.addBinaryBody(key, fileBody.getInputStream(), ContentType.DEFAULT_BINARY, fileBody.getFilename());
|
|
|
|
|
- } else {
|
|
|
|
|
- builder.addBinaryBody(key, String.valueOf(value).getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- HttpEntity entity = builder.build();
|
|
|
|
|
- httpPost.setEntity(entity);
|
|
|
|
|
- response = httpClient.execute(httpPost);
|
|
|
|
|
- if (response != null) {
|
|
|
|
|
- HttpEntity he = response.getEntity();
|
|
|
|
|
- if (he != null) {
|
|
|
|
|
- respContent = EntityUtils.toString(he, StandardCharsets.UTF_8);
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- log.error("对方响应的状态码不在符合的范围内!");
|
|
|
|
|
- throw new RuntimeException();
|
|
|
|
|
- }
|
|
|
|
|
- return respContent;
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- log.error("网络访问异常,请求url地址={},响应体={},error=", url, response, e);
|
|
|
|
|
- throw new RuntimeException();
|
|
|
|
|
- } finally {
|
|
|
|
|
- log.info("统一外网请求参数打印,post请求url地址={},响应={}", url, respContent);
|
|
|
|
|
- try {
|
|
|
|
|
- if (response != null) {
|
|
|
|
|
- response.close();
|
|
|
|
|
- }
|
|
|
|
|
- if (null != httpClient) {
|
|
|
|
|
- httpClient.close();
|
|
|
|
|
- }
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- log.error("请求链接释放异常", e);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|