Httpclient 实现带参文件上传
2024-09-07 23:19:03
这里直接贴出的是我封装好的doPostFile方法,httpclient 的版本是3.1。
public static String doPostFile(String url, Part[] parts){ String response = null;
PostMethod postMethod = new PostMethod(url);
try{
//设置请求实体
postMethod.setRequestEntity(new MultipartRequestEntity(parts,postMethod.getParams()));
HttpClient client = new HttpClient(); //设置连接超时限度
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
client.executeMethod(postMethod);
if(postMethod.getStatusCode() == HttpStatus.SC_OK){
response = new String(postMethod.getResponseBodyAsString().getBytes("utf-8"));
}
} catch(Exception e){
LOGGER.error("HttpServiceSender.doPostFile()",e.getMessage());
} finally {
postMethod.releaseConnection();
} return response;
}
调用此方法的代码,注意参数的传递方式
public String uploadImage(File file,String id,String name){ String result = null;
try{
//文件part,new FilePart添加的是File类型的文件,后面StringPart则为字符串参数
Part[] parts = { new FilePart("file", file),
new StringPart("id", id),
new StringPart("id",name)};
result = HttpServiceSender.doPostFile(SERVER+"service/resource/upload",parts); }catch(Exception e){
Logger.error("ImageService.uploadImage()", e.getMessage());
}
return result;
}
最新文章
- JSON例子异常分析
- window7下安装第三方包报错及解决
- 前端学习 第四弹: HTML(一)
- HTML基础(1)
- QString转换为char* (转)
- elecworks无法连接至协同服务器
- Pjax介绍及在asp.net MVC3中使用pjax的简单示例
- Java 网络编程(五) 使用TCP/IP的套接字(Socket)进行通信
- Ubuntu下Git的使用之创建版本库
- SharePoint 切换用户的小技巧
- R语言︱数据分组统计函数族——apply族用法与心得
- C# GUID介绍和的使用
- 1-7 hibernate关联关系映射
- 两个标签页定位第二个标签页元素时显示element not visible
- swift学习 引入三方遇到的问题
- MySQL中MyISAM与InnoDB区别
- Ubuntu重装VMwareTools
- JMeter 生成CSV文件中文变乱码的问题
- Win10系列:VC++ Direct3D模板介绍2
- mysql_提示 Lock wait timeout exceeded解决办法
热门文章
- 30 分钟学会 Flex 布局
- 最小生成树 (Minimum Spanning Tree,MST) --- Kruskal算法
- 四. Java继承和多态4. 多态和动态绑定
- 【bzoj1001】【最短路】【对偶图】【最大流转最小割】狼抓兔子题解
- 【转载】【面试经验】PHP中级面试题
- 分布式协调服务Zookeeper
- Uprobes: userspace probes >;= Linux 3.5
- 获取SQLServer的最完整数据字典的SQL语句
- 【log4j】springboot项目启动 ,使用的druid数据源,log4j报错 log4j:WARN Please initialize the log4j system properly.
- WEB安全漏洞与防范