位置: 编程技术 - 正文

HttpClient通过Post上传文件

编辑:rootadmin

推荐整理分享HttpClient通过Post上传文件,希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

在之前一段的项目中,使用Java模仿Http Post方式发送参数以及文件,单纯的传递参数或者文件可以使用URLConnection进行相应的处理。

HttpClient通过Post上传文件

但是项目中涉及到既要传递普通参数,也要传递多个文件(不是单纯的传递XML文件)。在网上寻找之后,发现是使用HttClient来进行响应的操作,起初尝试多次依然不能传递参数和传递文件,后来发现时因为当使用HttpClient时,不能使用request.getParameter()对普通参数进行获取,而要在服务器端使用Upload来进行操作。

HttpClient4.2 jar下载 : 客户端代码:

[java] view plaincopyimport java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.ParseException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; /** * * @author <a href="mailto:just_szl@hotmail.com"> Geray</a> * @version 1.0,-6- */ public class HttpPostArgumentTest2 { //file1与file2在同一个文件夹下 filepath是该文件夹指定的路径 public void SubmitPost(String url,String filename1,String filename2, String filepath){ HttpClient httpclient = new DefaultHttpClient(); try { HttpPost httppost = new HttpPost(url); FileBody bin = new FileBody(new File(filepath &#; File.separator &#; filename1)); FileBody bin2 = new FileBody(new File(filepath &#; File.separator &#; filename2)); StringBody comment = new StringBody(filename1); MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("file1", bin);//file1为请求后台的File upload;属性 reqEntity.addPart("file2", bin2);//file2为请求后台的File upload;属性 reqEntity.addPart("filename1", comment);//filename1为请求后台的普通参数;属性 httppost.setEntity(reqEntity); HttpResponse response = httpclient.execute(httppost); int statusCode = response.getStatusLine().getStatusCode(); if(statusCode == HttpStatus.SC_OK){ System.out.println("服务器正常响应....."); HttpEntity resEntity = response.getEntity(); System.out.println(EntityUtils.toString(resEntity));//httpclient自带的工具类读取返回数据 System.out.println(resEntity.getContent()); EntityUtils.consume(resEntity); } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) { } } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub HttpPostArgumentTest2 httpPostArgumentTest2 = new HttpPostArgumentTest2(); httpPostArgumentTest2.SubmitPost(" "test.xml","test.zip"," } } 服务端代码:[java] view plaincopypublic void receiveData(HttpServletRequest request, HttpServletResponse response) throws AppException{ PrintWriter out = null; response.setContentType("text/html;charset=UTF-8"); Map map = new HashMap(); FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); File directory = null; List<FileItem> items = new ArrayList(); try { items = upload.parseRequest(request); // 得到所有的文件 Iterator<FileItem> it = items.iterator(); while (it.hasNext()) { FileItem fItem = (FileItem) it.next(); String fName = ""; Object fValue = null; if (fItem.isFormField()) { // 普通文本框的&#; fName = fItem.getFieldName(); // fValue = fItem.getString(); fValue = fItem.getString("UTF-8"); map.put(fName, fValue); } else { // 获取上传文件的&#; fName = fItem.getFieldName(); fValue = fItem.getInputStream(); map.put(fName, fValue); String name = fItem.getName(); if(name != null && !("".equals(name))) { name = name.substring(name.lastIndexOf(File.separator) &#; 1); // String stamp = StringUtils.getFormattedCurrDateNumberString(); String timestamp_Str = TimeUtils.getCurrYearYYYY(); directory = new File(" directory.mkdirs(); String filePath = (" timestamp_Str&#; File.separator &#; name; map.put(fName &#; "FilePath", filePath); InputStream is = fItem.getInputStream(); FileOutputStream fos = new FileOutputStream(filePath); byte[] buffer = new byte[]; while (is.read(buffer) > 0) { fos.write(buffer, 0, buffer.length); } fos.flush(); fos.close(); map.put(fName &#; "FileName", name); } } } } catch (Exception e) { System.out.println("读取http请求属性&#;出错!"); // e.printStackTrace(); logger.error("读取http请求属性&#;出错"); } // 数据处理 try { out = response.getWriter(); out.print("{success:true, msg:'接收成功'}"); out.close(); } catch (IOException e) { e.printStackTrace(); } }

高博应诺培训班学习一个月总结 来到苏州已经有一个月了,在高博应诺学习也有一个月了,写点东西总结总结。首先说说老师的教学,老师的教学呢,感觉就是按部就班吧,按照安排

安卓R文件clean之后丢失,看我怎么找回来,是不是很奇葩 借鉴了两位仁兄的修改方法才解决:第一个提到:以下来自:

ActionBar基本简介 ActionBar基本简介学习笔记,欢迎指导。△本文介绍如下内容:→ActionBar的简介。→如何添加ActionBar。→为ActionBar设置按钮。→为子窗体设置回退键返回

标签: HttpClient通过Post上传文件

本文链接地址:https://www.jiuchutong.com/biancheng/386795.html 转载请保留说明!

上一篇:安卓手机安装gcc,编译c/c++(安卓手机安装推特)

下一篇:高博应诺培训班学习一个月总结(高博应诺官网)

  • 初级职称经济法怎么学
  • 城建税的征税范围不包括农村对不对
  • 5月份企业所得税
  • 税号都是数字吗
  • 公司注册资本认缴
  • 消费税的会计处理分录
  • 个人所得税分摊方式月扣除金额修改
  • 印花税购销合同计税依据70%
  • 刚成立的公司没有营业额怎么报税
  • 外籍个人工资薪金汇出境
  • 二手房的折旧率怎么算
  • 国有资产划转如何做账
  • 企业增资扩股如何办理
  • 个人到税务局怎么开增值税普通发票
  • 扣个税必须要交社保吗
  • 出口退税生产企业增值税附加税怎么申报
  • 开具的销项发票是否都要入收入科目吗?
  • 资产损失申报方式
  • 汇总纳税申报期
  • 合并报表怎么抵损益
  • 发票连号不许报销吗
  • 非独立核算分公司企业所得税
  • 定金转为货款金额需要特别约定吗?
  • 子公司借款给母公司要交税吗
  • 无形资产摊销方法应当反映其经济利益
  • 以前年度有亏损需要交增值税吗为什么
  • 增值税以前年度损益调整
  • 文化事业建设费税收优惠政策2023
  • 小规模建筑公司可以开劳务发票吗
  • 企业房产税怎么申报缴纳流程
  • 股权筹资的概念
  • 招投标交易服务平台有哪些
  • 如何查出账外收入
  • 帮别的公司代销产品,对方公司不开具发票怎么做帐
  • 固定资产怎么录入系统
  • 税务局返还的个税手续费需要缴纳增值税吗
  • window10的点在哪里
  • 银行承兑汇票有纸质的吗
  • 电脑如何关闭屏幕还在工作
  • 蔚蓝的海怎么样
  • vue项目中使用opencv
  • 销售材料计提存入什么
  • 公司未成立,发起人的责任
  • 阿尔卑斯山环保
  • thinkphp+vue
  • 往来款项清查账务处理
  • 购买方如何开具红字发票给销售方
  • 应收账款与营业收入比例分析
  • 股东入股资金如何记账
  • 小微企业城建税减免政策
  • 对于企业处理废水的建议
  • 燃气费计入什么科目
  • 企业办税人员如何实名认证
  • 接受捐赠的固定资产按什么入账
  • 去年一个月工资未计提直接计入成本,现在如何调整
  • 蔬菜批发成本怎么核算
  • 销售奖金交税由谁承担
  • 挂靠人员社保缴费分录如何做?
  • 会计调账是什么意思
  • 新企业的创建
  • win10安装mysql初始化失败原因
  • 系统盘gpt转mbr
  • mac支持的字体格式
  • 如何把windowsxp换成windows7
  • windows虚拟内存设置
  • 电脑word程序
  • win10控制面板如何卸载软件
  • virtualbox 虚拟化
  • javascriptcsdn
  • unity3d连接数据库
  • jquery插件大全
  • 编写自己的linux系统
  • unity 技术
  • 用shell写一个脚本,对文本中无序的一列数字排序
  • jquery更换图片
  • python import ssl
  • 不配合税务检查的法律责任
  • 外地经营预缴企业所得税
  • 车辆购置税查询官网四川
  • 税盘数字证书密码怎么查看
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设