java 使用HttpClient发送post请求,参数包括MultipartFile、Map以及File转MockMultipartFile

     遇到使用java调用其他系统的http接口时,发送的参数中有文件,不太好处理,如下总结了发送带文件的的http方法,发送的文件还需要先将File 转成MockMultipartFile 否则接收会报错。

关键的代码和依赖如下所示。

一、依赖

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
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.4.10</version>
</dependency>
  
<dependency>
    <groupId>commons-httpclient</groupId>
    <artifactId>commons-httpclient</artifactId>
    <version>3.1</version>
</dependency>
  
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.6</version>
</dependency>
 
<!-- File转MultipartFile使用 -->
<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.1.8.RELEASE</version>
         <scope>compile</scope>
</dependency>

 二、File转MockMultipartFile

1
2
3
4
5
6
7
8
9
10
11
12
File file = new File("d://1//test.png");
 
try {
    // 创建 FileInputStream
    FileInputStream input = new FileInputStream(file);
 
    // 使用 MockMultipartFile 初始化 MultipartFile
    MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "audio/mpeg", input);
    httpUtil.sendPostRequest("http://×××××××/add", multipartFile, formData);
} catch (Exception e) {
    e.printStackTrace();
}

 

三、http的发送方法

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
32
public static void sendPostRequest(String url, MultipartFile multipartFile, Map<String, String> formData) throws IOException {
        // Create an instance of CloseableHttpClient
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // Create a HttpPost object and set the URL
            HttpPost post = new HttpPost(url);
 
            // Create a MultipartEntityBuilder to build the multipart form data
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
 
            // Add form data fields to the builder
            for (Map.Entry<String, String> entry : formData.entrySet()) {
                builder.addTextBody(entry.getKey(), entry.getValue(), ContentType.create("text/plain", StandardCharsets.UTF_8));
            }
 
            // Add the file part to the builder
            builder.addBinaryBody("file", multipartFile.getInputStream(), ContentType.APPLICATION_OCTET_STREAM, multipartFile.getOriginalFilename());
 
            // Build the HttpEntity and set it to the HttpPost object
            HttpEntity entity = builder.build();
            post.setEntity(entity);
 
            // Execute the request and get the response
            try (CloseableHttpResponse response = httpClient.execute(post)) {
                // Process the response
                HttpEntity responseEntity = response.getEntity();
                if (responseEntity != null) {
                    String responseString = EntityUtils.toString(responseEntity);
                    System.out.println("Response: " + responseString);
                }
            }
        }
    }

 

posted @   万笑佛  阅读(250)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2023-01-03 java集合Collection操作
点击右上角即可分享
微信分享提示