这里直接贴出的是我封装好的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;
}

最新文章

  1. JSON例子异常分析
  2. window7下安装第三方包报错及解决
  3. 前端学习 第四弹: HTML(一)
  4. HTML基础(1)
  5. QString转换为char* (转)
  6. elecworks无法连接至协同服务器
  7. Pjax介绍及在asp.net MVC3中使用pjax的简单示例
  8. Java 网络编程(五) 使用TCP/IP的套接字(Socket)进行通信
  9. Ubuntu下Git的使用之创建版本库
  10. SharePoint 切换用户的小技巧
  11. R语言︱数据分组统计函数族——apply族用法与心得
  12. C# GUID介绍和的使用
  13. 1-7 hibernate关联关系映射
  14. 两个标签页定位第二个标签页元素时显示element not visible
  15. swift学习 引入三方遇到的问题
  16. MySQL中MyISAM与InnoDB区别
  17. Ubuntu重装VMwareTools
  18. JMeter 生成CSV文件中文变乱码的问题
  19. Win10系列:VC++ Direct3D模板介绍2
  20. mysql_提示 Lock wait timeout exceeded解决办法

热门文章

  1. 30 分钟学会 Flex 布局
  2. 最小生成树 (Minimum Spanning Tree,MST) --- Kruskal算法
  3. 四. Java继承和多态4. 多态和动态绑定
  4. 【bzoj1001】【最短路】【对偶图】【最大流转最小割】狼抓兔子题解
  5. 【转载】【面试经验】PHP中级面试题
  6. 分布式协调服务Zookeeper
  7. Uprobes: userspace probes >= Linux 3.5
  8. 获取SQLServer的最完整数据字典的SQL语句
  9. 【log4j】springboot项目启动 ,使用的druid数据源,log4j报错 log4j:WARN Please initialize the log4j system properly.
  10. WEB安全漏洞与防范