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提交文件/二进制数据的更多相关文章

  1. EasyUI Form提交后json数据IE上需要下载(转)

    EasyUI Form提交后json数据IE上需要下载(转)   在使用EasyUI的form中的submit方法时,返回json在IE中变成提示下载的问题,代码如下: $('#fileForm'). ...

  2. easyui form提交文件(上传图片和文件)

    <div id="dialogBtn"> <a class="easyui-linkbutton" href="#" on ...

  3. 利用Formdata实现form提交文件上传不跳转页面

    作者:幻月九十链接:https://www.zhihu.com/question/19631256/answer/119911045来源:知乎著作权归作者所有,转载请联系作者获得授权. $('form ...

  4. BLOB存储图片文件二进制数据是非对错

    子在一天一天虚度,生活也在一天一天中茫然 做人做事哪能尽如人意,付出多少收获多少虽然存在偏颇,但是不劳而获的心态是万万不对的,更不能去怨天尤人,低调为人.做好自己就可以了 改进你的系统的最好的方法是先 ...

  5. asp.net MVC中form提交和控制器接受form提交过来的数据

    1.cshtml页面form提交2.控制器处理表单提交数据4种方式方法1:使用传统的Request请求取值[HttpPost]public ActionResult AddNews(){    str ...

  6. 关于Python获取图片文件二进制数据的问题(获取为空)

    在搭建fastdfs文件系统的时候遇到了点问题,在测试上传文件数据流的时候,需要Python来获取本地文件的二进制流 from fdfs_client.client import Fdfs_clien ...

  7. .net core web api 与httpclient发送和接收文件及数据

    客户端 HttpClient var url = $"https://localhost:44323/api/values/posttest?resource_source=yangwwme ...

  8. js实现无刷新表单提交文件,将ajax请求转换为form请求方法

    最近在做项目的时候遇到一个需要上传文件的需求,因为ajax请求是无法上传二进制文件流的,所以只能用form表单提交,而form提交有一个问题就是会使页面刷新,本文解决了form表单提交文件时页面刷新的 ...

  9. form提交表单中包含time类型数据

    当数据库和实体类中含有date类型的数据时 ,form提交的时间数据只是string类型的,所以不能直接写入到java实体类和数据库,经过百度找到了解决方法 ,特地挪过来: 在controller中增 ...

随机推荐

  1. 使用CSS禁止textarea调整大小功能的方法

    这篇文章主要介绍了使用CSS禁止textarea调整大小功能的方法,禁止可以调整textarea大小功能的方法很简单,使用CSS的resize属性即可,需要的朋友可以参考下 如果你使用谷歌浏览器或火狐 ...

  2. hdu 2255 奔小康赚大钱 最大权匹配KM

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子.这可是一件大事 ...

  3. poj 1269 Intersecting Lines

    题目链接:http://poj.org/problem?id=1269 题目大意:给出四个点的坐标x1,y1,x2,y2,x3,y3,x4,y4,前两个形成一条直线,后两个坐标形成一条直线.然后问你是 ...

  4. Matlab设置网格线密度(坐标精度)

    1.不精确 set(gca,'XMinorTick','on') 这样的话知识x轴显示了细的密度,网格线并没有变. 2.精确 set(gca,'xtick',-1:0.1:1); set(gca,'y ...

  5. Leetcode#103 Binary Tree Zigzag Level Order Traversal

    原题地址 基本数据结构操作,二叉树的层次遍历. 代码: vector<vector<int> > zigzagLevelOrder(TreeNode *root) { vect ...

  6. 已有a,b两个链表,每个链表中的结点包括学号、成绩。要求把两个链表合并,按学号升序排列

    1.我的思路先将b链表连接在a链表的后面,这个很容易实现,将a链表最后的结点中的p.next改为指向b链表的头结点即可. 再将这个新链表用选择排序即可. 代码如下: #include<stdio ...

  7. 关于“无法定位程序输入点gzdirect于动态链接库zlib1.dll”的问题

    费劲N多力气编译通过之后,最后启动程序过程中却突然得到“无法定位程序输入点gzdirect于动态链接库zlib1.dll”的问题, 分析究其原因是定位不到zlib1.dll,都知道,程序在找dll的时 ...

  8. Docker学习资料

    官方Docker:https://www.docker.com/ CSDN Docker : http://docker.csdn.net/ Docker中文社区:http://www.docker. ...

  9. cf div2 237 D

    D. Minesweeper 1D time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...

  10. SGU 104

    104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB PROBLEM Yo ...