java发送post请求上传Content-Type: application/json和Content-Type: image/jpeg数据

java发送post请求上传Content-Type: application/json和Content-Type: image/jpeg数据

上传示例:

POST https://xxxxx?pid=112&method=unlock_log
Authorization: Bearer 3543sf34-3434-4f56-ad06-23gh45j6785
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="json"
Content-Type: application/json;charset=UTF-8
[
    {
            "id": "324324432",
            "isLocal": 1,
            "addr": "上海北",
            "type": "抓拍机",
            "ip": "192.168.1.22",
            "password": "9999999",
            "community": "小区",
     }
]

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="captureImage"; filename="1.jpeg"
Content-Type: image/jpeg
图片二进制数据...
public string httppost(HttpServletRequest request) {
    try {
        //获取request里的所有部分
        Collection<Part> parts = request.getParts();
        String json = null;
        String imageBase = null;
        for (Iterator<Part> iterator = parts.iterator(); iterator.hasNext(); ) {
            Part part = iterator.next();
            System.out.println("名称========" + part.getName());
            if ("json".equals(part.getName())) {//上传数据格式中的name的值
                //解析json对象
            InputStream in =part.getInputStream();
            byte recvs[] = CommonUtils.toByteArray(in);
            json = new String(recvs, "UTF-8");
            json = json.replaceAll("\r|\n", "").replaceAll(" ", "");
            System.out.println("json==" + json);
            }
            if ("captureImage".equals(part.getName())) {//上传数据格式中的name的值
                    //获取文件二进制数据
                InputStream inputStream = part.getInputStream();
                byte recvs[] = CommonUtils.toByteArray(inputStream);
                BASE64Encoder encoder = new BASE64Encoder();
                imageBase = encoder.encode(recvs).replaceAll("\r|\n", "");
            System.out.println("json==" + imageBase);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return "success";
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值