HttpUtil.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package com.ydtech.utils;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.http.HttpRequest;
  4. import cn.hutool.http.HttpResponse;
  5. import cn.hutool.json.JSONUtil;
  6. import com.alibaba.fastjson.JSONObject;
  7. import org.apache.commons.httpclient.methods.multipart.FilePart;
  8. import org.slf4j.Logger;
  9. import java.io.*;
  10. import java.net.HttpURLConnection;
  11. import java.net.URL;
  12. import java.util.HashMap;
  13. import java.util.Map;
  14. /**
  15. * @ClassName HttpUtil
  16. * @Description: TODO
  17. * @Author
  18. * @Date 2021/5/25
  19. **/
  20. public class HttpUtil {
  21. /**
  22. * 发起https请求并获取结果
  23. *
  24. * @param requestUrl 请求地址
  25. * @param requestMethod 请求方式(GET、POST)
  26. * @param outputStr 提交的数据
  27. * @return JSONObject(通过JSONObject.get ( key)的方式获取json对象的属性值)
  28. */
  29. public static JSONObject HttpRequest(String request, String RequestMethod, String output) {
  30. @SuppressWarnings("unused")
  31. JSONObject jsonObject = null;
  32. StringBuffer buffer = new StringBuffer();
  33. try {
  34. //建立连接
  35. URL url = new URL(request);
  36. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  37. connection.setDoOutput(true);
  38. connection.setDoInput(true);
  39. connection.setUseCaches(false);
  40. // connection.setRequestMethod(RequestMethod);
  41. //设置请求内容编码格式
  42. connection.setRequestProperty("content-type", "application/json;charset=UTF-8");
  43. connection.setRequestProperty("Accept", "application/json;charset=UTF-8");
  44. if (output != null) {
  45. OutputStream out = connection.getOutputStream();
  46. out.write(output.getBytes("UTF-8"));
  47. out.close();
  48. }
  49. //流处理
  50. InputStream input = connection.getInputStream();
  51. // InputStreamReader inputReader = new InputStreamReader(input,"UTF-8");
  52. BufferedReader reader = new BufferedReader(new InputStreamReader(input, "GBK"));
  53. String line;
  54. while ((line = reader.readLine()) != null) {
  55. buffer.append(line);
  56. }
  57. //关闭连接、释放资源
  58. reader.close();
  59. input.close();
  60. connection.disconnect();
  61. } catch (Exception e) {
  62. }
  63. return jsonObject;
  64. }
  65. /**
  66. * 带header和json作为body的post
  67. * @param url
  68. * @param bodyStr body传的json
  69. *
  70. * @author: lig
  71. * @date: 2023年08月14日 0014
  72. */
  73. public static String sendPost(String url,String bodyStr){
  74. System.err.println("请求[post、json、body]参数:"+bodyStr);
  75. HttpRequest request = HttpRequest.post(url)
  76. .header("Content-Type", "application/json")//以json的方式传递
  77. //     .header("authorization","Bearer abcdefghijklmnopqrstuvwxyz")
  78. .body(bodyStr);
  79. HttpResponse response = request.execute();
  80. String respStr = response.body();
  81. System.err.println("返回[post、json、body]参数:"+ respStr);
  82. return respStr;
  83. }
  84. /**
  85. * 带header和json作为body的post
  86. * @param url
  87. * @param bodyStr body传的json
  88. *
  89. * @author: lig
  90. * @date: 2023年08月14日 0014
  91. */
  92. public static String sendPost(String url,String bodyStr,Logger log,String apiName){
  93. log.info(apiName+"请求[post、json、body]参数:"+bodyStr);
  94. HttpRequest request = HttpRequest.post(url)
  95. .header("Content-Type", "application/json")//以json的方式传递
  96. //     .header("authorization","Bearer abcdefghijklmnopqrstuvwxyz")
  97. .body(bodyStr);
  98. HttpResponse response = request.execute();
  99. String respStr = response.body();
  100. log.info(apiName+"返回[post、json、body]参数:"+respStr);
  101. return respStr;
  102. }
  103. /**
  104. * post from
  105. * @param url
  106. * @param formMap 附件需要传的类型FilePart
  107. *
  108. * @author: lig
  109. * @date: 2023-08-21
  110. */
  111. public static String sendPostFrom(String url,Map<String, Object> formMap,Logger log,String apiName){
  112. System.err.println("请求[post、json、body]参数:"+ JSONUtil.toJsonStr(formMap));
  113. log.info(apiName + ",请求[post、json、body]参数:"+ JSONUtil.toJsonStr(formMap));
  114. HttpRequest request = HttpRequest.post(url);
  115. request.header("Content-Type", "multipart/form-data; ");
  116. // // 添加文件参数
  117. // FilePart file1 = new FilePart("file1", new File("path/to/file1.jpg"));
  118. request.form(formMap);
  119. HttpResponse response = request.execute();
  120. // 处理服务器响应
  121. if (response.isOk()) {
  122. String respStr = response.body();
  123. log.info(apiName + "返回[post、json、body]参数:"+ respStr);
  124. return respStr;
  125. }
  126. return null;
  127. }
  128. /**
  129. * post from
  130. * @param url
  131. * @param formMap 附件需要传的类型FilePart
  132. *
  133. * @author: lig
  134. * @date: 2023-08-21
  135. */
  136. public static String sendPostFrom(String url,Map<String, Object> formMap){
  137. System.err.println("请求[post、json、body]参数:"+ JSONUtil.toJsonStr(formMap));
  138. // log.info(apiName + ",请求[post、json、body]参数:"+ JSONUtil.toJsonStr(formMap));
  139. HttpRequest request = HttpRequest.post(url);
  140. request.header("Content-Type", "multipart/form-data; ");
  141. // // 添加文件参数
  142. // FilePart file1 = new FilePart("file1", new File("path/to/file1.jpg"));
  143. request.form(formMap);
  144. HttpResponse response = request.execute();
  145. // 处理服务器响应
  146. if (response.isOk()) {
  147. String respStr = response.body();
  148. // log.info(apiName + "返回[post、json、body]参数:"+ respStr);
  149. return respStr;
  150. }
  151. return null;
  152. }
  153. public static void main(String[] args) {
  154. String url = "https://devyun.guorenpcic.com/api/grecar/photo/upload";
  155. Map<String, Object> formMap = new HashMap<>();
  156. formMap.put("businessNo","2322");
  157. formMap.put("operatorCode","32323");
  158. formMap.put("comCode","22323");
  159. formMap.put("fileGroup","DZ");
  160. try {
  161. FilePart file1 = new FilePart("file1", new File("D:/Desktop/123.png"));
  162. formMap.put("file",file1);
  163. } catch (FileNotFoundException e) {
  164. throw new RuntimeException(e);
  165. }
  166. sendPostFrom(url,formMap);
  167. }
  168. }