okhttp基本服务请求用法

此次主要介绍用okhttp进行服务器的基本访问方法如:post、get的基本请求或者post模拟表单上传文件。

第一步引入需要的jar:

1
2
3
4
5
6
7
8
9


<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.10.0</version>
</dependency>


普通的数据请求POST方式提交String

1
2
3
4
5
6
7
8
9
10
11


String requestInfo = "Nihao";
Request request = new Request.Builder()
.url("")
.post(RequestBody.create(MediaType.parse("text/x-markdown;charset=utf-8"), requestInfo))
.build();
OkHttpClient okHttpClient = new OkHttpClient();
Response response = okHttpClient.newCall(request).execute();


POST方式提交单个文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18


OkHttpClient client = new OkHttpClient();
String filePath = "D:" + File.separator + "workCatalog" + File.separator + "1.mp4";
RequestBody requestBody = RequestBody.create(MediaType.parse("mp4"), filePath);
RequestBody requestBody1 = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("请求携带的参数","123")
//请求的名称,文件的名称,//创建RequestBody,把上传的文件放入
.addFormDataPart("file", "xx.mp4",requestBody)
.build();
Request request = new Request.Builder()
.url("")
.post(requestBody1)
.build();
Response response = client.newCall(request).execute();


POST方式提交多个文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31


List<String> strList = new ArrayList<>();
String filePath = "D:" + File.separator + "workCatalog" + File.separator + "1.mp4";
String filePath2 = "D:" + File.separator + "workCatalog" + File.separator + "timg.jpg";
String filePath3 = "D:" + File.separator + "workCatalog" + File.separator + "timg2.jpg";
strList.add(filePath);
strList.add(filePath2);
strList.add(filePath3);
MultipartBody.Builder builder = new MultipartBody.Builder();
for (int i = 0; i < strList.size(); i++) {
File file = new File(strList.get(i));
//获得文件名称
String fileName = file.getName();
//获得文件后缀名
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
builder.addFormDataPart("file" + i, file.getName(), RequestBody.create(MediaType.parse(suffix), file));
}
builder.addFormDataPart("cityId", "cityId");
RequestBody requestBody = builder.build();
Request.Builder builderReq = new Request.Builder();
//"http:localhost:8081/api/wx/cloud"
//http:localhost:8082/legaPeople.do?action=imageS
Request request = builderReq.url("http:localhost:8081/api/wx/cloud")
.post(requestBody)
.build();
OkHttpClient okHttpClient = new OkHttpClient();
okhttp3.Response response = okHttpClient.newCall(request).execute();
log.info("response:" + response);


服务端接收处理文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26


List<MultipartFile> multipartFile = m.getFiles("file");
String originalFilename = "";
if (!multipartFile.isEmpty()) {
for (int i = 0; i < multipartFile.size(); i++) {
//获取原始文件名称
originalFilename = multipartFile.get(i).getOriginalFilename();
//将原始文件名前缀和后缀分开放入数组
String[] fileNames = originalFilename.split("\\.");
//文件名后缀
String suffixName = fileNames[fileNames.length - 1];
//保存文件名
String saveName = UUID.randomUUID().toString().replace("-", "") + "." + suffixName;
//保存文件路径
String filePathName = "D:" + File.separator + "workCatalog" + File.separator + "timg" + File.separator;
File file = new File(filePathName);
File file1 = new File(filePathName + saveName);
if (!file.exists()) {
file.mkdirs();
}
FileUtils.copyInputStreamToFile(multipartFile.get(i).getInputStream(), file1);
}
}