一、需求
客户端给服务端发送PUT请求,请求中既包含文件数据,又包含其他json数据。
二、简单说明
客户端采用HttpClient,服务器为SpringBoot + SSM框架。
HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
三、版本说明
- HttpClient,版本4.5.12,以下是maven依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
<version>4.5.12</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.12</version>
</dependency>
- SpringBoot,版本为2.2.6,具体依赖就不列举了。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
</parent>
四、具体实现
客户端
/** 注册接口模型接口:模拟测试 */
public static String doPut() throws IOException {
String modelId = "777";
/** restful风格 */
String url = "http://127.0.0.1:8888/product/models/" + modelId;
/** token身份信息 */
String authorization = "VIFTBzS7or0jkB4mdXg3mzjlaveWIv";
/** 获取客户端client, 生成put请求 */
HttpClient client = HttpClients.createDefault();
HttpPut put = new HttpPut(url);
/** 设置头部信息 */
put.addHeader("Authorization","Bearer "+authorization);
put.addHeader("Accept","*/*");
/** 创建 文件上传实体类构建者 */
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
/** 设置UTF-8编码 */
builder.setMode(HttpMultipartMode.RFC6532);
builder.setCharset(Charset.forName("UTF-8"));
/** 传输本地一个文件到服务器端 */
String filePath = "G:\\test.zip";
/** 获取上传文件名 */
String[] split = filePath.split("\\\\");
String fileName = split[split.length - 1];
/** 准备字节输入流 */
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
/** 文件流添入builder,注意contentType类型! */
/** 后台接口,@RequestParam(value="model") MultipartFile model */
builder.addBinaryBody("model", bis, ContentType.MULTIPART_FORM_DATA, fileName);
/** 如果需要添加其他数据,使用 builder.addTextBody(),不能使用 new StringEntity(),Entity仅有一个! */
/** 设置其他数据的字符编码 */
ContentType contentType = ContentType.create("text/plain", Charset.forName("UTF-8"));
builder.addTextBody("model_name", "模型"+modelId, contentType);
builder.addTextBody("username", "zhaocy", contentType);
/** 构建实体,set到put请求中 */
HttpEntity entity = builder.build();
put.setEntity(entity);
/** 发送请求 */
HttpResponse response = client.execute(put);
/** 获取响应实体 */
HttpEntity responseEntity = response.getEntity();
return EntityUtils.toString(responseEntity);
}
服务器端接口
@ApiOperation("注册生产模型接口API")
@PutMapping(value="models/{id}", produces="multipart/form-data;charset=UTF-8")
public String registerProductModel2(
@PathVariable(value="id") Integer id,
@RequestParam(value="model") MultipartFile model,
@RequestParam(value="username") String username,
HttpServletRequest request
){
return this.productService.registerProductModel(id,model,model,username).toString();
}
五、注意编码设置
客户端
builder.setMode(HttpMultipartMode.RFC6532);
builder.setCharset(Charset.forName(“UTF-8”));
ContentType contentType = ContentType.create(“text/plain”,Charset.forName(“UTF-8”));
builder.addTextBody(“model_name”, “模型”+modelId, contentType);