java 发送 http 文件 post,form-data格式的数据,MultipartEntityBuilder addTextBody中文乱码

平常我们对接第三方都是以json的数据进行数据交互的,这次第三方接口只支持form-data格式的表单数据,传json数据对方不支持,通过百度和尝试各种方案最终完美解决,后期再慢慢优化吧。还有一个问题,数据中包含中文的户,到第三方是乱码的,经过百度参考前辈的经验,完没解决addTextBody乱码问题。记录下工作中遇到的一个小问题!

请求通过httpClient上传文件

package com.example.demo.controller;

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.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

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

/**
 * @program: demo
 * @description: 描述
 * @author: 
 * @date: 2022-09-08 14:07
 **/


public class TestFormData {
    public static void main(String args[]) throws Exception {

        String url = "http://127.0.0.1/subject/file";
        File file = new File("/Users/Desktop/5555.png");
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
            HttpPost httpPost = new HttpPost(url);
            //HttpMultipartMode.RFC6532参数的设定是为避免文件名为中文时乱码
            MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.RFC6532);
            httpPost.addHeader("Authorization", "11222233333");//头部放文件上传的head可自定义
            //builder.addTextBody("name", "张三"); 汉字会乱码 需要用下面的方法处理
            ContentType contentType = ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);
            StringBody stringBody = new StringBody("李四5",contentType);
            builder.addPart("name", stringBody);

            builder.addBinaryBody("photo", file);//其余参数,可自定义
            builder.addTextBody("subject_type", "1");

            builder.addTextBody("start_time", "1662691418");
            builder.addTextBody("end_time", "1662720218");

            HttpEntity entity = builder.build();
            httpPost.setEntity(entity);
            response = httpClient.execute(httpPost);// 执行提交
            HttpEntity responseEntity = response.getEntity();//接收调用外部接口返回的内容
            // 通过EntityUtils中的toString方法将结果转换为字符串
            String result = EntityUtils.toString(responseEntity);
            System.out.println(result);//返回的json数据 之后自己的业务处理
        } catch (Exception e) {
            //logger.error("上传文件失败:",e);
            System.out.println("LLLLLLLl");
        } finally {//处理结束后关闭httpclient的链接
            try {
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

postman这样传的数据格式

 

httpcomponent框架MultipartEntityBuilder addTextBody中文乱码

// 使用addPart+ StringBody代替addTextBody,解决中文乱码

// builder.addTextBody(entry.getKey(), entry.getValue());

ContentType contentType = ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);
StringBody stringBody = new StringBody(entry.getValue(),contentType);
builder.addPart(entry.getKey(), stringBody);

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值