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;
}
HttpClient Post Form提交文件/二进制数据的更多相关文章
- EasyUI Form提交后json数据IE上需要下载(转)
EasyUI Form提交后json数据IE上需要下载(转) 在使用EasyUI的form中的submit方法时,返回json在IE中变成提示下载的问题,代码如下: $('#fileForm'). ...
- easyui form提交文件(上传图片和文件)
<div id="dialogBtn"> <a class="easyui-linkbutton" href="#" on ...
- 利用Formdata实现form提交文件上传不跳转页面
作者:幻月九十链接:https://www.zhihu.com/question/19631256/answer/119911045来源:知乎著作权归作者所有,转载请联系作者获得授权. $('form ...
- BLOB存储图片文件二进制数据是非对错
子在一天一天虚度,生活也在一天一天中茫然 做人做事哪能尽如人意,付出多少收获多少虽然存在偏颇,但是不劳而获的心态是万万不对的,更不能去怨天尤人,低调为人.做好自己就可以了 改进你的系统的最好的方法是先 ...
- asp.net MVC中form提交和控制器接受form提交过来的数据
1.cshtml页面form提交2.控制器处理表单提交数据4种方式方法1:使用传统的Request请求取值[HttpPost]public ActionResult AddNews(){ str ...
- 关于Python获取图片文件二进制数据的问题(获取为空)
在搭建fastdfs文件系统的时候遇到了点问题,在测试上传文件数据流的时候,需要Python来获取本地文件的二进制流 from fdfs_client.client import Fdfs_clien ...
- .net core web api 与httpclient发送和接收文件及数据
客户端 HttpClient var url = $"https://localhost:44323/api/values/posttest?resource_source=yangwwme ...
- js实现无刷新表单提交文件,将ajax请求转换为form请求方法
最近在做项目的时候遇到一个需要上传文件的需求,因为ajax请求是无法上传二进制文件流的,所以只能用form表单提交,而form提交有一个问题就是会使页面刷新,本文解决了form表单提交文件时页面刷新的 ...
- form提交表单中包含time类型数据
当数据库和实体类中含有date类型的数据时 ,form提交的时间数据只是string类型的,所以不能直接写入到java实体类和数据库,经过百度找到了解决方法 ,特地挪过来: 在controller中增 ...
随机推荐
- @修饰符--python中的装饰器
装饰器模式可以在不影响其他对象的情况下,以动态.透明的方式给单个对象添加职责,也能够处理那些可以撤销的职责.经常用于日志记录.性能测试等场合. 想象一下这个很常见的场景,你写了一个方法只提供给以登陆的 ...
- Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- [百度空间] [转]DLL地狱及其解决方案
DLL地狱及其解决方案 原作者:Ivan S Zapreev 译者:陆其明概要 本文将要介绍DLL的向后兼容性问题,也就是著名的“DLL Hell”问题.首先我会列出自己的研究结果,其中包括其它一些研 ...
- [geeksforgeeks] Count the number of occurrences in a sorted array
Count the number of occurrences in a sorted array Given a sorted array arr[] and a number x, write a ...
- UML快速指南(摘要)转载
UML 概述: UML是一个通用的建模语言.它最初开始捕捉到复杂的软件和非软件系统的行为,现在它已经成为一个OMG标准. UML提供元素和组件的复杂系统支持的要求. UML遵循面向对象的概念和方法.因 ...
- YARN应用程序的开发步骤
开发基于YARN的应用程序需要开发客户端程序和AppMaster程序: 我们基于程序自带的例子来实现提交application 到YARN的ResourceManger. Distributed Sh ...
- 10 个让人惊讶的 jQuery 插件
说是让人惊讶,你可能会觉得我们没见过世面,但这里提及的一些 jQuery 的插件的确平时比较少见,用的人应该更少. Grid portfolio 使用竖排方式显示条目信息,现在很流行的的内容布局方式. ...
- (翻译)Google Guava Cache
翻译自Google Guava Cache This Post is a continuation of my series on Google Guava, this time covering G ...
- 一张思维导图说明jQuery的AJAX请求机制
比文字描述清晰多了吧?而且越是复杂的逻辑,思维导图的作用就越大,同时对阅读源码也是一种快捷的方法. 看不清楚的话可以右键,在新标签页中打开图片,或者保存本地.
- LCT小结
LCT真是灵活好用… LCT的基本思想与树链剖分差不多,都是把树剖成一条条链,只不过LCT用的是SPLAY维护的,而且,SPLAY的链是会变化的,不像剖分是定死的. LCT最重要的操作就是access ...