此处发请求的是用httpclient4,请自己下载所需要的jar包。

发post请求,并得到数据。

String url = "http://localhost:8080/lee";
url = url+ "/query/action/export.action";
String exportFilePath = "lee"+".csv."; final HttpClient httpClient = new DefaultHttpClient();
final HttpPost post = new HttpPost(url);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("leeSmart", leeSmart));//发post请求的参数
post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
final HttpResponse response = httpClient.execute(post);//得到返回的response
final int code = response.getStatusLine().getStatusCode();
final HttpEntity entity = response.getEntity();//得到entity
if (entity != null && code < 400) {
InputStream inputStream = entity.getContent();//得到从服务器端返回的数据流
long length = entity.getContentLength();
if(length<=0) return;
int len = (int)length;
byte[] b = new byte[len];
int readCount = 0;
//建议用以下方式读inputStream为b赋值
while (readCount < len) {
readCount += inputStream.read(b, readCount, len - readCount);
}
//在客户端生成文件。更高效的做法是,在服务器端传过来一个压缩后的btye[],然后在客户端解压,减少传输数据。
try {
FileOutputStream fo1 = null;
fo1 = new FileOutputStream(exportFilePath);
fo1.write(b);
}
fo1.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}

在action中接请求的方法export(),并返回数据流

try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
response.setCharacterEncoding("UTF-8");
String leeSmart = request.getParameter("leeSmart");//前台传过来的post参数
byte[] b = null;
try{
List ret = serivce.query("select * from dual");//得到查询结果集
//将ret放到sb中
StringBuilder sb = new StringBuilder();
//.......对结果集进行处理,并转成字节数组
                      b = sb.toString().getByte();
}catch(Exception e){
e.printStackTrace();
}
//如果方便,可以把b字节数组压缩一下,这样传的数据会比较小一些。
//将字节数组放到response中,并返回到客户端。
try {
             response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String("random".getBytes("UTF-8"),"ISO-8859-1"));
            response.addHeader("Content-Length", "" + b.length);
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(b);
            toClient.flush();
             toClient.close();
        } catch (Exception e) {
             e.printStackTrace();
         }finally{
            
        }


												

用apache的httpclient发请求和接受数据的更多相关文章

  1. PHP通过curl向其它服务器发请求并返回数据

    在很多时候,我们都需要请求第三方的服务器来获取一些数据,比如token,比如百度的主动推送,那么我们的php如何实现向第三方服务器发请求呢?我们可以通过curl来实现 首先定义请求的url,然后创建h ...

  2. Ajax向Controller发送请求并接受数据需要注意的一个细节

    想用Ajax想向Controller发送请求和接收返回的字符等等.Controller中要使用@ResponseBody注解. <script type="text/javascrip ...

  3. java apache commons HttpClient发送get和post请求的学习整理(转)

    文章转自:http://blog.csdn.net/ambitiontan/archive/2006/01/06/572171.aspx HttpClient 是我最近想研究的东西,以前想过的一些应用 ...

  4. httpClient模拟浏览器发请求

    一.介绍 httpClient是Apache公司的一个子项目, 用来提高高效的.最新的.功能丰富的支持http协议的客户端编程工具包.完成可以模拟浏览器发起请求行为. 二.简单使用例子 : 模拟浏览器 ...

  5. java模拟http请求上传文件,基于Apache的httpclient

    1.依赖 模拟http端的请求需要依赖Apache的httpclient,需要第三方JSON支持,项目中添加 <dependency> <groupId>org.apache& ...

  6. Java使用Apache的HttpClient组件发送https请求

    如果我们直接通过普通的方式对https的链接发送请求,会报一个如下的错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.Va ...

  7. 使用HttpClient实现对第三方服务器的请求并接受返回数据

    /* * 创建日期 2017-4-7 * * TODO 要更改此生成的文件的模板,请转至 * 窗口 - 首选项 - Java - 代码样式 - 代码模板 */ package com.enfo.int ...

  8. 使用apache的HttpClient进行http通讯,隐藏的HTTP请求头部字段是如何自动被添加的

    我们用apache的HttpClient这个库消费云端的Restful API时,一般都需要两次HTTP调用,第一次获得某种token,比如获取防止跨域请求伪造攻击Cross-site request ...

  9. Java HttpClient伪造请求之简易封装满足HTTP以及HTTPS请求

    HttpClient简介 HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 jav ...

随机推荐

  1. 贴板子系列_1-exgcd

    exgcd ll exgcd(ll a,ll b,ll &x,ll &y) { ) { x=;y=;return a; } ll r=exgcd(b,a%b,x,y); ll t=x; ...

  2. 大数据量查询优化——数据库设计、SQL语句、JAVA编码

    数据库设计方面: 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将 ...

  3. 第 12 章 命令模式【Command Pattern】

    以下内容出自:<<24种设计模式介绍与6大设计原则>> 今天讲命令模式,这个模式从名字上看就很简单,命令嘛,老大发命令,小兵执行就是了,确实是这个意思,但是更深化了,用模式来描 ...

  4. 3D触控简介:建立数字刻度应用及快速活动栏

    苹果公司通过 iPhone 6s 和 6s Plus 引入了与手机互动的全新方式:按压手势.你也许知道,苹果智能手表和苹果笔记本电脑早已具备这一功能,只是名称略有不同,为力感触控(Force Touc ...

  5. Ansj分词双数组Trie树实现与arrays.dic词典格式

    http://www.hankcs.com/nlp/ansj-word-pairs-array-tire-tree-achieved-with-arrays-dic-dictionary-format ...

  6. Android如何在一个线性布局里完美显示两个listview啊?

    复写一个listView ,在你布局文件中使用此view: <ScrollView android:layout_width="fill_parent" android:la ...

  7. 备份及还原Xcode的模拟器

    http://blog.csdn.net/it_magician/article/details/8749876 每次更新或者重装Xcode之后,最麻烦的莫过于各个模拟器的安装了,因为下载速度实在让人 ...

  8. 【转】Windows7 下安装 JDK 7 时版本冲突问题解决

    原文网址:http://wxl24life.iteye.com/blog/1966058 自己电脑上一直用的 JDK 版本是 1.6,今天决定更新到 1.7,在安装 JDK 1.7 后,控制台输入 j ...

  9. Windows SharePoint Services 默认母版页

    转:http://msdn.microsoft.com/zh-cn/library/ms467402(v=office.12).aspx 最终用户可以自定义的 Windows SharePoint S ...

  10. JavaScript修改表中的内容

    例子: <?php ?> <html> <head> <meta http-equiv="Content-Type" content=&q ...