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

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

在实际工作中有一个这样的需求,前端需要上传一个图片文件到第三方的服务服务中,但是前端又不能直接调用第三方的接口上传,只能通过我们服务作为中间服务上传图片到第三方服务中,即前端调我们的接口,我们再调第三方的接口进行上传。

使用postman工具发送multipart/form-data带有Json文件的Post请求,文件内容其实就是json字符串,这种请求通过postman发送,他给你处理,但是你需要做接口化测试就得偶用代码来实现,不是使用他的工具,就需要你自己写代码了

搭建步骤

1.导入依赖

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.3.6</version>
</dependency>

2.请求代码处理

package com.sean.formdata.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
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.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.charset.Charset;

/**
 * @author : sean
 * @version V1.0
 * @Project: formdata
 * @Package com.sean.formdata.controller
 * @date Date : 2021年09月28日 21:29
 * @Description:
 */
@RestController
@Slf4j
public class FormDataController {


    @PostMapping("/formdata")
    public void formData(@RequestBody MultipartFile file,String url)
    {
        try {
              MultipartEntityBuilder builder = MultipartEntityBuilder.create();
              //设置编码格式
              builder.setCharset(Charset.forName("UTF8"));
              //设置请求体的参数
              builder.addTextBody("username","zhangsan");
              builder.addTextBody("password","lisi");
              builder.addTextBody("age","18");
              builder.addTextBody("email","zhangsan@163.com");
              //把文件放到请求体中
              builder.addBinaryBody("file",file.getInputStream(), ContentType.MULTIPART_FORM_DATA,file.getOriginalFilename());
              //构建请求实体
              HttpEntity entity = builder.build();

            //  创建Post方式请求
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(entity);

            CloseableHttpClient httpClient = HttpClients.createDefault();
            //发送请求
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);

            //状态码
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            //msg
            String reasonPhrase = httpResponse.getStatusLine().getReasonPhrase();
            //data
            String responseBody = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

3.遇到的问题

在访问第三方提供的接口时,有可能是http或https请求,如果是http请求直接使用代码是没有问题的,如果是https请求的话就不行,需要提供CA证书,但是又没有证书的情况下,就需要绕过Https的证书认证。

/**
     * 获取https连接(不验证证书)
     *
     * @return
     */
    private static CloseableHttpClient getHttpsClient() {
        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.<ConnectionSocketFactory>create();
        ConnectionSocketFactory plainSF = new PlainConnectionSocketFactory();
        registryBuilder.register("http", plainSF);
        // 指定信任密钥存储对象和连接套接字工厂
        try {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            // 信任任何链接
            TrustStrategy anyTrustStrategy = new TrustStrategy() {

                @Override
                public boolean isTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException {
                    // TODO Auto-generated method stub
                    return true;
                }
            };
            SSLContext sslContext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, anyTrustStrategy).build();
            LayeredConnectionSocketFactory sslSF = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            registryBuilder.register("https", sslSF);
        } catch (KeyStoreException e) {
            throw new RuntimeException(e);
        } catch (KeyManagementException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        Registry<ConnectionSocketFactory> registry = registryBuilder.build();
        // 设置连接管理器
        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry);
        // 构建客户端
        return HttpClientBuilder.create().setConnectionManager(connManager).build();
    }
上一篇:6.创建型模式(建造者模式)


下一篇:设计模式之建造者模式