import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpRequest;
*** 簡單 get 請求
String jsonStr = HttpUtil.get(url);
*** 帶參數, 帶請求頭 post
cn.hutool.json.JSONObject param = JSONUtil.createObj();
param.put("project_id", projectId);
param.put("project_name", projectName);
String resultJson = HttpRequest.post(getUrl)
.header("token", token)
.body(param)
.execute().body();
*** get 獲取圖片
byte[] bytes = HttpRequest.get(getUrl)
.header("token", token)
.execute().bodyBytes();
FileOutputStream fos = new FileOutputStream("d:/mpl.jpg");
fos.write(bytes);
fos.close();
下載到固定位置, d 盤
注: 跟普通請求不一樣的地方 , 在返回的是bodyBytes() , 開始返回的是String , 結果就亂碼了, 返回的String 再轉byte 也是行不通的, 因爲獲取前就亂碼了
*** 獲取圖片, 二進制流輸出
public String blockCallBack(HttpServletResponse response) throws Exception {
byte[] bytes = HttpRequest.get(getUrl)
.header("Content-Type","application/octet-stream")
.header("token", token)
.header("content-disposition", "attachment;filename=" + URLEncoder.encode("test", "UTF-8"))
.execute().bodyBytes();
OutputStream outputStream = response.getOutputStream();
outputStream.write(bytes);
outputStream.flush();
outputStream.close();
}
*** 還可以使用 HttpUtil工具類
//帶參post
String result= HttpUtil.post("http://10.1.3.4:8030/api/inspection/uploadVideo", param);