平常我們對(duì)接第三方都是以json的數(shù)據(jù)進(jìn)行數(shù)據(jù)交互的,這次第三方接口只支持form-data格式的表單數(shù)據(jù),傳json數(shù)據(jù)對(duì)方不支持,通過(guò)百度和嘗試各種方案最終完美解決,后期再慢慢優(yōu)化吧。還有一個(gè)問(wèn)題,數(shù)據(jù)中包含中文的戶,到第三方是亂碼的,經(jīng)過(guò)百度參考前輩的經(jīng)驗(yàn),完沒(méi)解決addTextBody亂碼問(wèn)題。記錄下工作中遇到的一個(gè)小問(wèn)題!
請(qǐng)求通過(guò)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參數(shù)的設(shè)定是為避免文件名為中文時(shí)亂碼
MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.RFC6532);
httpPost.addHeader("Authorization", "11222233333");//頭部放文件上傳的head可自定義
//builder.addTextBody("name", "張三"); 漢字會(huì)亂碼 需要用下面的方法處理
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);//其余參數(shù),可自定義
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);// 執(zhí)行提交
HttpEntity responseEntity = response.getEntity();//接收調(diào)用外部接口返回的內(nèi)容
// 通過(guò)EntityUtils中的toString方法將結(jié)果轉(zhuǎn)換為字符串
String result = EntityUtils.toString(responseEntity);
System.out.println(result);//返回的json數(shù)據(jù) 之后自己的業(yè)務(wù)處理
} catch (Exception e) {
//logger.error("上傳文件失?。?,e);
System.out.println("LLLLLLLl");
} finally {//處理結(jié)束后關(guān)閉httpclient的鏈接
try {
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
postman這樣傳的數(shù)據(jù)格式
?
httpcomponent框架MultipartEntityBuilder addTextBody中文亂碼
// 使用addPart+ StringBody代替addTextBody,解決中文亂碼
// builder.addTextBody(entry.getKey(), entry.getValue());文章來(lái)源:http://www.zghlxwxcb.cn/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);文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-544099.html
到了這里,關(guān)于java 發(fā)送 http 文件 post,form-data格式的數(shù)據(jù),MultipartEntityBuilder addTextBody中文亂碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!