spring mvc 服务端接收httpclient post 参数无法接收问题

在spring mvc 服务端接收post请求时,通过html 表单提交的时候,服务端能够接收到参数的值。但是使用httpclient4.3构造post请求,却无法接收到参数的值。

spring 代码:

    @RequestMapping(value = "login.do", method = RequestMethod.POST)
    @ResponseBody
    public String login(String username, String password) throws Exception {
        return username + ":" + password;
    }
表单代码:

<form action="http://localhost:8080/test/login.do" id="frm" method="post">
    name:<input type="text" name="username" id="username"/>   </br>
    psword:<input type="text" name="password" id="password"/>  </br>
    <input id="submit" type="submit" />
 </form>
httpclient4.3发送post代码:

    @Test
    public void testMultipartPost() throws IOException {
        HttpPost httpPost = new HttpPost("http://localhost:8080/test/login.do");
        try {
            HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
            CloseableHttpClient httpClient = httpClientBuilder.build();
            RequestConfig config = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000).build();
            httpPost.setConfig(config);
            MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
            multipartEntityBuilder.setCharset(Charset.forName("UTF-8"));
            multipartEntityBuilder.addTextBody("username", "taozi");
            multipartEntityBuilder.addTextBody("password", "123");
            HttpEntity httpEntity = multipartEntityBuilder.build();
            httpPost.setEntity(httpEntity);
            HttpResponse response = httpClient.execute(httpPost);
            System.out.println(EntityUtils.toString(response.getEntity()));
        } finally {
            httpPost.releaseConnection();
        }
    }
一直在查找原因,为什么通过httpclient4.3构造的post请求,服务端无法接收到传输的参数。比较与html的差异,发现httpclient构造的请求使用的是multipart形式。而表单上传使用的是默认形式的编码,x-www-form-urlencoded,所以表单能够成功。现在找到问题了,将httpclient的构造代码,改为x-www-form-urlencoded编码上传,

@Test
    public void testUrlencodedPost() throws IOException {
        HttpPost httpPost = new HttpPost("http://localhost:8080/test/login.do");
        try {
            CloseableHttpClient client = HttpClients.createDefault();
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username", "taozi"));
            params.add(new BasicNameValuePair("password", "123"));
            HttpEntity httpEntity = new UrlEncodedFormEntity(params, "UTF-8");
            httpPost.setEntity(httpEntity);
            CloseableHttpResponse response = client.execute(httpPost);
            System.out.println(EntityUtils.toString(response.getEntity()));
        } finally {
            httpPost.releaseConnection();
        }
    }
现在服务端能够正常的接收到请求了,现在总结一下表单两种编码的形式

application/x-www-form-urlencoded   空格转换为 "+" 加号,特殊符号转换为 ASCII HEX 值

multipart/form-data    不对字符进行编码,使用二进制数据传输,一般用于上传文件,非文本的数据传输。


spring mvc如果要接收 multipart/form-data 传输的数据,应该在spring上下文配置

<bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">       
    </bean>
这样服务端就既可以接收multipart/form-data 传输的数据,也可以接收application/x-www-form-urlencoded传输的文本数据了。



### 回答1: 在Java后端,使用HttpClient库进行POST请求提交文件流,包括以下步骤: 1. 导入HttpClient库的依赖: ```xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency> ``` 2. 创建HttpClient对象: ```java CloseableHttpClient httpClient = HttpClients.createDefault(); ``` 3. 创建HttpPost对象,并设置URL和请求体: ```java HttpPost httpPost = new HttpPost("http://example.com/upload"); File file = new File("path/to/file"); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addPart("file", new FileBody(file)); HttpEntity multipart = builder.build(); httpPost.setEntity(multipart); ``` 4. 执行POST请求并获取响应: ```java CloseableHttpResponse response = httpClient.execute(httpPost); ``` 在服务端接收文件流的代码如下: 1. 创建Spring Boot Controller并监听指定URL: ```java @RestController public class FileUploadController { @PostMapping("/upload") public String handleFileUpload(@RequestParam("file") MultipartFile file) { // 处理文件流 // ... return "File uploaded successfully."; } } ``` 2. 接收到请求时,Spring框架会自动将文件流绑定到`MultipartFile`对象,并调用`handleFileUpload`方法进行处理。 以上就是使用HttpClient库进行Java后端HTTP POST请求提交文件流以及服务端接收文件流的简单示例。 ### 回答2: 在Java后端中使用HttpClient进行post请求提交文件流的步骤如下: 1. 引入相关的依赖,如Apache HttpClient: ```xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency> ``` 2. 创建HttpClient对象: ```java CloseableHttpClient httpClient = HttpClients.createDefault(); ``` 3. 创建HttpPost请求对象: ```java HttpPost httpPost = new HttpPost(url); ``` 其中,`url`为服务端接收文件流的接口地址。 4. 创建File实例或通过其他方式获取要上传的文件流: ```java File file = new File("path/to/file"); FileBody fileBody = new FileBody(file); ``` 5. 创建HttpEntity,并将文件对象添加到其中: ```java MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addPart("file", fileBody); HttpEntity httpEntity = builder.build(); ``` 6. 设置请求的Entity为刚创建的HttpEntity: ```java httpPost.setEntity(httpEntity); ``` 7. 发送post请求并获取响应: ```java CloseableHttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity responseEntity = httpResponse.getEntity(); ``` 以上是使用HttpClient在Java后端进行post请求提交文件流的基本步骤。 在服务端接收文件流的代码示例如下(以Spring Boot为例): 1. 创建用于接收文件流的接口: ```java @PostMapping("/upload") public void uploadFile(MultipartFile file) { // 处理文件流的业务逻辑 } ``` 2. 在接口中使用MultipartFile对象接收文件流,Spring Boot会自动将上传的文件流封装成MultipartFile对象。 3. 可以在接口中进行一些与文件相关的处理,如保存文件、读取文件内容等。 以上就是使用Java后端的HttpClinet进行文件流提交以及在服务端接收文件流的简单介绍。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值