java+addbinarybody_HttpClient Post Form提交文件/二进制数据

HttpClient httpClient = HttpClients.createDefault();

HttpPost httppost = new HttpPost(url);

MultipartEntityBuilder mEntityBuilder = MultipartEntityBuilder.create();

//byte[] postBody

mEntityBuilder.addBinaryBody(postName, postBody);

//提交文件

//File file = new File("test");

//mEntityBuilder.addBinaryBody("name", file);

mEntityBuilder.addTextBody("name", "Value");

httppost.setEntity(mEntityBuilder.build());

HttpResponse responce = httpClient.execute(httppost);

不写成接口:可以直接写在一起

HttpEntity reqEntity = MultipartEntityBuilder.create()

.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)

.addPart("multipartFile", bin)

.addPart("userId", userId).setCharset(CharsetUtils.get("UTF-8")).build();

不带参数时:可以直接定义指定的entity

File file = new File("somefile.txt");

FileEntity reqEntity = new FileEntity(file, ContentType.create("text/plain", "UTF-8"));

byte[] b;

ByteArrayEntity entity = new ByteArrayEntity(b) ;

下面是我自己定义的接口:

/**

* Http request :Post

*

* @param url

* @param postBody(Byte)

* @param postName

* @param params

* @param heads

* @param timeOut(Millisecond)

* @return String of request result

*/

public static String postFile(String url, byte[] postBody, String postName, Map params,

Map heads, Integer timeOut) throws HttpErrorException {

String reStr = "";

try {

HttpClient httpClient = HttpClients.createDefault();

HttpPost httppost = new HttpPost(url);

MultipartEntityBuilder mEntityBuilder = MultipartEntityBuilder.create();

mEntityBuilder.addBinaryBody(postName, postBody);

if (params != null) {

// text params

for (Entry e : params.entrySet()) {

mEntityBuilder.addTextBody(e.getKey(), e.getValue());

}

}

httppost.setEntity(mEntityBuilder.build());

if (heads != null) {

// 一般要求プロパティを設定します

for (Entry e : heads.entrySet()) {

httppost.addHeader(e.getKey(), e.getValue());

}

}

// set Timeout

RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeOut)

.setConnectTimeout(timeOut).setSocketTimeout(timeOut).build();

httppost.setConfig(requestConfig);

// get responce

HttpResponse responce = httpClient.execute(httppost);

// get http status code

int resStatu = responce.getStatusLine().getStatusCode();

if (resStatu == HttpStatus.SC_OK) {

// get result data

HttpEntity entity = responce.getEntity();

reStr = EntityUtils.toString(entity);

}

else {

log.error(url + ": resStatu is " + resStatu);

throw new HttpErrorException(url, "resStatu is" + resStatu);

}

}

catch (ConnectionPoolTimeoutException e) {

log.error("http post throw ConnectionPoolTimeoutException", e);

throw new HttpErrorException(url, " throw timeout");

}

catch (ConnectTimeoutException e) {

log.error("http post throw ConnectTimeoutException", e);

throw new HttpErrorException(url, " throw timeout");

}

catch (SocketTimeoutException e) {

log.error("http post throw SocketTimeoutException", e);

throw new HttpErrorException(url, " throw timeout");

}

catch (HttpErrorException e) {

throw e;

}

catch (Exception e) {

log.error("http post throw Exception", e);

throw new HttpErrorException(url, " throw Exception");

}

return reStr;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值