java发送post请求上传Content-Type: application/json和Content-Type: image/jpeg数据
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";
}
更多推荐
已为社区贡献2条内容
所有评论(0)