平常我们对接第三方都是以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());文章来源:https://www.toymoban.com/news/detail-544099.html
ContentType contentType = ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);
StringBody stringBody = new StringBody(entry.getValue(),contentType);
builder.addPart(entry.getKey(), stringBody);文章来源地址https://www.toymoban.com/news/detail-544099.html
到了这里,关于java 发送 http 文件 post,form-data格式的数据,MultipartEntityBuilder addTextBody中文乱码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!