|
|
@@ -1,14 +1,18 @@
|
|
|
package com.ydtech.utils;
|
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.apache.commons.httpclient.methods.multipart.FilePart;
|
|
|
|
|
|
|
|
|
-import java.io.BufferedReader;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.InputStreamReader;
|
|
|
-import java.io.OutputStream;
|
|
|
+import java.io.*;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* @ClassName HttpUtil
|
|
|
@@ -66,4 +70,78 @@ public class HttpUtil {
|
|
|
return jsonObject;
|
|
|
}
|
|
|
|
|
|
-}
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 带header和json作为body的post
|
|
|
+ * @param url
|
|
|
+ * @param bodyStr body传的json
|
|
|
+ *
|
|
|
+ * @author: lig
|
|
|
+ * @date: 2023年08月14日 0014
|
|
|
+ */
|
|
|
+ public static String sendPost(String url,String bodyStr){
|
|
|
+ System.err.println("请求[post、json、body]参数:"+bodyStr);
|
|
|
+ HttpRequest request = HttpRequest.post(url)
|
|
|
+ .header("Content-Type", "application/json")//以json的方式传递
|
|
|
+// .header("authorization","Bearer abcdefghijklmnopqrstuvwxyz")
|
|
|
+ .body(bodyStr);
|
|
|
+
|
|
|
+ HttpResponse response = request.execute();
|
|
|
+ String respStr = response.body();
|
|
|
+ System.err.println("返回[post、json、body]参数:"+ respStr);
|
|
|
+
|
|
|
+ return respStr;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * post from
|
|
|
+ * @param url
|
|
|
+ * @param formMap 附件需要传的类型FilePart
|
|
|
+ *
|
|
|
+ * @author: lig
|
|
|
+ * @date: 2023-08-21
|
|
|
+ */
|
|
|
+ public static String sendPostFrom(String url,Map<String, Object> formMap){
|
|
|
+ System.err.println("请求[post、json、body]参数:"+ JSONUtil.toJsonStr(formMap));
|
|
|
+ HttpRequest request = HttpRequest.post(url);
|
|
|
+ request.header("Content-Type", "multipart/form-data; ");
|
|
|
+
|
|
|
+// // 添加文件参数
|
|
|
+// FilePart file1 = new FilePart("file1", new File("path/to/file1.jpg"));
|
|
|
+ request.form(formMap);
|
|
|
+
|
|
|
+
|
|
|
+ HttpResponse response = request.execute();
|
|
|
+ // 处理服务器响应
|
|
|
+ if (response.isOk()) {
|
|
|
+ String respStr = response.body();
|
|
|
+ System.err.println("返回[post、json、body]参数:"+ respStr);
|
|
|
+ return respStr;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String url = "https://devyun.guorenpcic.com/api/grecar/photo/upload";
|
|
|
+ Map<String, Object> formMap = new HashMap<>();
|
|
|
+ formMap.put("businessNo","2322");
|
|
|
+ formMap.put("operatorCode","32323");
|
|
|
+ formMap.put("comCode","22323");
|
|
|
+ formMap.put("fileGroup","DZ");
|
|
|
+
|
|
|
+ try {
|
|
|
+ FilePart file1 = new FilePart("file1", new File("D:/Desktop/123.png"));
|
|
|
+ formMap.put("file",file1);
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ sendPostFrom(url,formMap);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|