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 org.slf4j.Logger; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; /** * @ClassName HttpUtil * @Description: TODO * @Author * @Date 2021/5/25 **/ public class HttpUtil { /** * 发起https请求并获取结果 * * @param requestUrl 请求地址 * @param requestMethod 请求方式(GET、POST) * @param outputStr 提交的数据 * @return JSONObject(通过JSONObject.get ( key)的方式获取json对象的属性值) */ public static JSONObject HttpRequest(String request, String RequestMethod, String output) { @SuppressWarnings("unused") JSONObject jsonObject = null; StringBuffer buffer = new StringBuffer(); try { //建立连接 URL url = new URL(request); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); // connection.setRequestMethod(RequestMethod); //设置请求内容编码格式 connection.setRequestProperty("content-type", "application/json;charset=UTF-8"); connection.setRequestProperty("Accept", "application/json;charset=UTF-8"); if (output != null) { OutputStream out = connection.getOutputStream(); out.write(output.getBytes("UTF-8")); out.close(); } //流处理 InputStream input = connection.getInputStream(); // InputStreamReader inputReader = new InputStreamReader(input,"UTF-8"); BufferedReader reader = new BufferedReader(new InputStreamReader(input, "GBK")); String line; while ((line = reader.readLine()) != null) { buffer.append(line); } //关闭连接、释放资源 reader.close(); input.close(); connection.disconnect(); } catch (Exception e) { } 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; } /** * 带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,Logger log,String apiName){ log.info(apiName+"请求[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(); log.info(apiName+"返回[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 formMap,Logger log,String apiName){ System.err.println("请求[post、json、body]参数:"+ JSONUtil.toJsonStr(formMap)); log.info(apiName + ",请求[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(); log.info(apiName + "返回[post、json、body]参数:"+ respStr); return respStr; } return null; } /** * post from * @param url * @param formMap 附件需要传的类型FilePart * * @author: lig * @date: 2023-08-21 */ public static String sendPostFrom(String url,Map formMap){ System.err.println("请求[post、json、body]参数:"+ JSONUtil.toJsonStr(formMap)); // log.info(apiName + ",请求[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(); // log.info(apiName + "返回[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 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); } }