HuTool

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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值