HTTP上传数据的方式和OKHttp的实现
1.form-data
即http请求中的multipart/form-data,它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开。既可以上传键值对,也可以上传文件。当上传的字段是文件时,会有Content-Type来说明文件类型;content-disposition,用来说明字段的一些信息;由于有boundary隔离,所以multipart/form-data既可以上传文件,也可以上传键值对,它采用了键值对的方式,所以可以上传多个文件。
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("name", "Tome")
.addFormDataPart("from", "China")
.addFormDataPart("hobby", "football")
.addFormDataPart("image", "logo-square.png", RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png")))
.build();
Request request = new Request.Builder()
.url("http://192.168.10.66/api/upload?name=242910b319c841bc804c53ef72d39990.jpg&ids=6143&type=2")
.method("POST", body)
.addHeader("x-sessionkey", "59534B58-2BC8-49BF-8B0E-2E3872C9F1DB")
.addHeader("x-userid", "752332")
.addHeader("User-Agent", "android")
.addHeader("x-tonce", "1592286822038")
.addHeader("x-timestamp", "1592286822038")
.build();
Response response = client.newCall(request).execute();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
2.x-www-form-urlencoded
即application/x-www-from-urlencoded,会将表单内的数据转换为键值对,比如name=Tom&from=China&hobby=football。
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "name=Tom&from=China&hobby=football");
Request request = new Request.Builder()
.url("http://192.168.10.66/api/upload?name=242910b319c841bc804c53ef72d39990.jpg&ids=6143&type=2")
.method("POST", body)
.addHeader("x-sessionkey", "59534B58-2BC8-49BF-8B0E-2E3872C9F1DB")
.addHeader("x-userid", "752332")
.addHeader("User-Agent", "android")
.addHeader("x-tonce", "1592286822038")
.addHeader("x-timestamp", "1592286822038")
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build();
Response response = client.newCall(request).execute();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
multipart/form-data与x-www-form-urlencoded区别:
- multipart/form-data:既可以上传文件等二进制数据,也可以上传表单键值对,只是最后会转化为一条信息;
- x-www-form-urlencoded:只能上传键值对,并且键值对都是间隔分开的。
3.raw
可以上传任意格式的文本,可以上传text、json、xml、html等。
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = RequestBody.create(mediaType, "Hello world");
Request request = new Request.Builder()
.url("http://192.168.10.66/api/upload?name=242910b319c841bc804c53ef72d39990.jpg&ids=6143&type=2")
.method("POST", body)
.addHeader("x-sessionkey", "59534B58-2BC8-49BF-8B0E-2E3872C9F1DB")
.addHeader("x-userid", "88752332")
.addHeader("User-Agent", "android")
.addHeader("x-tonce", "1592286822038")
.addHeader("x-timestamp", "1592286822038")
.addHeader("Content-Type", "text/plain")
.build();
Response response = client.newCall(request).execute();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
4.Binary
相当于Content-Type:application/octet-stream,只可以上传二进制数据,通常用来上传文件,由于没有键值。所以,一次只能上传一个文件。这个HTTP报文的body都是文件的信息,除此之外没有额外多余的信息。如下面是传一张jpg图片:
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("image/jpeg");
RequestBody body = RequestBody.create(mediaType, "<file contents here>");
Request request = new Request.Builder()
.url("http://192.168.10.66/api/upload?name=242910b319c841bc804c53ef72d39990.jpg&ids=6143&type=2")
.method("POST", body)
.addHeader("x-sessionkey", "59534B58-2BC8-49BF-8B0E-2E3872C9F1DB")
.addHeader("x-userid", "88752332")
.addHeader("User-Agent", "android")
.addHeader("x-tonce", "1592286822038")
.addHeader("x-timestamp", "1592286822038")
.addHeader("Content-Type", "image/jpeg")
.build();
Response response = client.newCall(request).execute();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_40763897/article/details/106806721
![](https://res.hc-cdn.com/ecology/9.3.130/v2_resources\images\global\blog-dialog.png)
华为开发者空间发布
让每位开发者拥有一台云主机
- 点赞
- 收藏
- 关注作者
评论(0)