HttpClient + SpringBoot(SSM)

一、需求

  客户端给服务端发送PUT请求,请求中既包含文件数据,又包含其他json数据。

二、简单说明

  客户端采用HttpClient,服务器为SpringBoot + SSM框架。
  HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。

三、版本说明

  1. 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>
  1. 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);

服务器端
SpringBoot设置编码,以下是参考链接:
链接一
链接二

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值