| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- 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<String, Object> 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<String, Object> 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<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);
- }
- }
|