Java中 HttpClient 发送multipart/form-data带有Json文件的Post请求

使用postman工具发送multipart/form-data带有Json文件的Post请求,文件内容其实就是json字符串,这种请求通过postman发送,他给你处理,但是你需要做接口化测试就得偶用代码来实现,不是使用他的工具,就需要你自己写代码了
在这里插入图片描述
1、首先弄清楚你需要得格式:

  • 像我这个这种格式就是可以的(关于文件类型)
    在这里插入图片描述
    这个最难最重要的就是处理上面的这个文件。
  • 需要的两个jar包,我是使用maven工具直接导入的。根据自己情况来就可以了
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.3.6</version>
        </dependency>

请求处理的代码:

package com.hy.controller;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@Controller
public class FaceDemo02 {
    public static void main(String[] args) throws ClientProtocolException, IOException {
        // 文件sTestsetFile:是我存放文件所在的项目的相对对位置
        String sTestsetFile=System.getProperty("user.dir")+ File.separator+"src"+ File.separator+"main"+ File.separator+"resources"+ File.separator+"static"+ File.separator+"images"+File.separator+"ceshi.jpg";
        String sURL="http://192.168.4.10:80/api/form";

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost uploadFile = new HttpPost(sURL);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        //builder.setCharset(Charset.forName("uft-8"));//设置请求的编码格式
        builder.addTextBody("msg_id", "1029", ContentType.TEXT_PLAIN);
        builder.addTextBody("lib_id", "1", ContentType.TEXT_PLAIN);
        builder.addTextBody("person_name", "xiaoming", ContentType.TEXT_PLAIN);

        // 把文件加到HTTP的post请求中
        File f = new File(sTestsetFile);
        builder.addBinaryBody(
                "img",
                new FileInputStream(f),
                ContentType.APPLICATION_OCTET_STREAM,
                f.getName()
        );

        HttpEntity multipart = builder.build();
        uploadFile.setEntity(multipart);
        CloseableHttpResponse response = httpClient.execute(uploadFile);
        HttpEntity responseEntity = response.getEntity();
        String sResponse= EntityUtils.toString(responseEntity, "UTF-8");
        //打印请求返回的结果
        System.out.println("Post 返回结果"+sResponse);
    }
}


我测试使用的是可以的 ,文件也是能传输的,可以参考一下!!!

发布了81 篇原创文章 · 获赞 26 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_44411569/article/details/100049277