通过一个线程来实现图片/文件的传输(本示例代码段是图片的传输):
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient okHttpClient = new OkHttpClient();
File file = new File(photoOutputUri.getPath());
String filename = photoOutputUri.getPath().substring(photoOutputUri.getPath().lastIndexOf("/") + 1);
RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addPart(Headers.of(
"Content-Disposition",
"form-data; name=\"getUpTime\""),
RequestBody.create(null, "2019-3-29"))
.addPart(Headers.of(
"Content-Disposition",
"form-data; name=\"originalData\"; filename=\"" + filename + "\""), fileBody)
.build();
String url = "http://192.168.13.53:8888/mainPage/solution";
System.out.println("----------------------------------------------------");
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("text", "failure upload!" + e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i("text", "success upload!");
String json = response.body().string();
Log.i("success........", "成功" + json);
}
});
}
}).start();
这里使用新建线程的方式是为了避免主线程过载,引起报错或是程序无法正常运行,响应超时等问题
RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
首先建立RequestBody,其中MediaType是互联网媒体类型,这里的application/octet-stream是二进制流数据,通常的文件上传和下载都用此类型
另外, multipart/form-data这个类型是在表单中进行文件上传的时候就需要应用此类格式
.addPart(Headers.of(
"Content-Disposition",
"form-data; name=\"originalData\"; filename=\"" + filename + "\""), fileBody)
这段是将上传的图片进行封装
传输Jason文件示例:
new Thread(new Runnable() {
@Override
public void run() {
String url = "http://192.168.13.53:8888/mainPage/items";
String msg = "{\"items\":[{\"itemId\":\"63655535\",\"content\":\"\\u003cimg class=\\\"snapshot\\\" src=\\\"https://qb-oss.bytededu.cn/snapshot/0e9abbfe5b2f159de7f6aff1393fcfda-375.png\\\" width=\\\"335\\\" height=\\\"311\\\" font_size=\\\"12\\\" data-spec=\\\"375-335-311,320-280-338,360-320-311,411-371-284,460-420-284,534-494-257,768-728-257\\\" \\u003e\",\"answer\":\"\\u003cimg class=\\\"snapshot\\\" src=\\\"https://qb-oss.bytededu.cn/snapshot/9ad935517d160f3adf6b7c2273e435e0-375.png\\\" width=\\\"335\\\" height=\\\"398\\\" font_size=\\\"12\\\" data-spec=\\\"375-335-398,320-280-392,360-320-392,411-371-398,460-420-365,534-494-338,768-728-338\\\" \\u003e\",\"hint\":\"\\u003cimg class=\\\"snapshot\\\" src=\\\"https://qb-oss.bytededu.cn/snapshot/5dcd9c037b7a8d58767e0af0ae6be27b-375.png\\\" width=\\\"335\\\" height=\\\"135\\\" font_size=\\\"12\\\" data-spec=\\\"375-335-135,320-280-135,360-320-135,411-371-135,460-420-108,534-494-81,768-728-81\\\" \\u003e\",\"remark\":\"\\u003cimg class=\\\"snapshot\\\" src=\\\"https://qb-oss.bytededu.cn/snapshot/b90a0b7984403ffa312fd81d5ca9dcfd-375.png\\\" width=\\\"335\\\" height=\\\"81\\\" font_size=\\\"12\\\" data-spec=\\\"375-335-81,320-280-81,360-320-81,411-371-54,460-420-54,534-494-54,768-728-27\\\" \\u003e\",\"subject\":2},{\"itemId\":\"669758137\",\"content\":\"\\u003cimg class=\\\"snapshot\\\" src=\\\"https://qb-oss.bytededu.cn/snapshot/53c78f44194d8564f2a94ea58906d6e0-375.png\\\" width=\\\"335\\\" height=\\\"232\\\" font_size=\\\"12\\\" data-spec=\\\"375-335-232,320-280-286,360-320-232,411-371-232,460-420-205,534-494-178,768-728-124\\\" \\u003e\",\"answer\":\"\\u003cimg class=\\\"snapshot\\\" src=\\\"https://qb-oss.bytededu.cn/snapshot/26213ba6812317b5f8b8a760cdfc8830-375.png\\\" width=\\\"335\\\" height=\\\"27\\\" font_size=\\\"12\\\" data-spec=\\\"375-335-27,320-280-27,360-320-27,411-371-27,460-420-27,534-494-27,768-728-27\\\" \\u003e\",\"hint\":\"\\u003cimg class=\\\"snapshot\\\" src=\\\"https://qb-oss.bytededu.cn/snapshot/e299c1930920e946023fb55119c2feda-375.png\\\" width=\\\"335\\\" height=\\\"135\\\" font_size=\\\"12\\\" data-spec=\\\"375-335-135,320-280-135,360-320-135,411-371-135,460-420-108,534-494-81,768-728-81\\\" \\u003e\",\"remark\":\"\\u003cimg class=\\\"snapshot\\\" src=\\\"https://qb-oss.bytededu.cn/snapshot/0ad7412106d44cb92faa60eadbcd95da-375.png\\\" width=\\\"335\\\" height=\\\"81\\\" font_size=\\\"12\\\" data-spec=\\\"375-335-81,320-280-81,360-320-81,411-371-54,460-420-54,534-494-54,768-728-27\\\" \\u003e\",\"subject\":2},{\"itemId\":\"4220693\",\"content\":\"\\u003cimg class=\\\"snapshot\\\" src=\\\"https://qb-oss.bytededu.cn/snapshot/9e3a21f3ded24126d6f6d1c9535dcf4a-375.png\\\" width=\\\"335\\\" height=\\\"269\\\" font_size=\\\"12\\\" data-spec=\\\"375-335-269,320-280-323,360-320-296,411-371-269,460-420-269,534-494-242,768-728-215\\\" \\u003e\",\"answer\":\"\\u003cimg class=\\\"snapshot\\\" src=\\\"https://qb-oss.bytededu.cn/snapshot/5299347fcd22fee176c0f7f2778e190b-375.png\\\" width=\\\"335\\\" height=\\\"153\\\" font_size=\\\"12\\\" data-spec=\\\"375-335-153,320-280-207,360-320-180,411-371-153,460-420-153,534-494-153,768-728-153\\\" \\u003e\",\"hint\":\"\\u003cimg class=\\\"snapshot\\\" src=\\\"https://qb-oss.bytededu.cn/snapshot/24b4b1993aff02dca46bfd2aae02c8f5-375.png\\\" width=\\\"335\\\" height=\\\"216\\\" font_size=\\\"12\\\" data-spec=\\\"375-335-216,320-280-216,360-320-216,411-371-216,460-420-162,534-494-162,768-728-135\\\" \\u003e\",\"remark\":\"\\u003cimg class=\\\"snapshot\\\" src=\\\"https://qb-oss.bytededu.cn/snapshot/333f6b20d2a336571efd4fe1462f3e57-375.png\\\" width=\\\"335\\\" height=\\\"124\\\" font_size=\\\"12\\\" data-spec=\\\"375-335-124,320-280-180,360-320-124,411-371-96,460-420-96,534-494-96,768-728-64\\\" \\u003e\",\"subject\":2},{\"itemId\":\"37265214\",\"content\":\"\\u003cimg class=\\\"snapshot\\\" src=\\\"https://qb-oss.bytededu.cn/snapshot/140690c9bf227a3b9a0d0daaef08d3ef-375.png\\\" width=\\\"335\\\" height=\\\"396\\\" font_size=\\\"12\\\" data-spec=\\\"375-335-396,320-280-423,360-320-396,411-371-396,460-420-369,534-494-369,768-728-342\\\" \\u003e\",\"answer\":\"\\u003cimg class=\\\"snapshot\\\" src=\\\"https://qb-oss.bytededu.cn/snapshot/761c05d59177cbf17280da8756d35349-375.png\\\" width=\\\"335\\\" height=\\\"297\\\" font_size=\\\"12\\\" data-spec=\\\"375-335-297,320-280-378,360-320-324,411-371-297,460-420-297,534-494-297,768-728-243\\\" \\u003e\",\"hint\":\"\",\"remark\":\"\",\"subject\":2},{\"itemId\":\"35241890\",\"content\":\"\\u003cimg class=\\\"snapshot\\\" src=\\\"https://qb-oss.bytededu.cn/snapshot/31ed07207a268fce4b6a6dfeb0addb28-375.png\\\" width=\\\"335\\\" height=\\\"343\\\" font_size=\\\"12\\\" data-spec=\\\"375-335-343,320-280-371,360-320-343,411-371-315,460-420-315,534-494-315,768-728-287\\\" \\u003e\",\"answer\":\"\\u003cimg class=\\\"snapshot\\\" src=\\\"https://qb-oss.bytededu.cn/snapshot/982d819072e60c4375742e185480cc10-375.png\\\" width=\\\"335\\\" height=\\\"289\\\" font_size=\\\"12\\\" data-spec=\\\"375-335-289,320-280-345,360-320-289,411-371-289,460-420-289,534-494-261,768-728-233\\\" \\u003e\",\"hint\":\"\",\"remark\":\"\",\"subject\":2}],\"BaseResp\":{\"error\":{\"code\":0,\"type\":0,\"title\":\"\",\"e_message\":\"\",\"nlp\":\"\",\"schema\":\"\",\"confirm_button\":\"\",\"cancel_button\":\"\",\"error_comment\":\"\"},\"logId\":\"202112041434510102020871482607AD35\",\"StatusMessage\":\"\",\"StatusCode\":0,\"Extra\":{\"logId\":\"202112041434510102020871482607AD35\"}}}";
String base640 = msg.substring(0, msg.length() / 2);
System.out.println("------------------------------------------------------------");
System.out.println("000" + base640);
System.out.println(msg.length());
System.out.println(base640.length());
OkHttpClient okHttpClient = new OkHttpClient();
RequestBody requestBody = new FormBody.Builder() //构建body
.add("items", msg)
.add("u_id", "1")
.add("dev_id", "1")
.add("request_id", "1")
.add("search_id", "1")
.add("ques_num", "1")
.add("ans_num", "1")
.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d("MainActivityPost", "连接失败" + e.getLocalizedMessage());
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
System.out.println(response.protocol() + "::::" + response.message());
System.out.println(response.body().toString());
}
});
}
}).start();
Jason文件此处使用表单来进行上传,不再赘述,如有疑问或不足,欢迎在评论区大家一起讨论