此处发请求的是用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{
            
        }


												

最新文章

  1. Database first with EntityFramework (Migration)安装和升级
  2. Jquery实现让滚动条始终保持在最下方
  3. Adroid_Spinner_ArrayAdapter
  4. 【转】命令行使用7zip
  5. Python 面向对象基础
  6. 类图class的关联关系(聚合、组合)
  7. JSP中EL很常用,怎样使用大于号、小于号、等于号等
  8. python练习题-day18
  9. webservice学习教程(三)--
  10. pycharm工具配置
  11. bootstrap 下拉菜单自动向上向下弹起
  12. Vue.js 模板指令
  13. DB通用类:SQL Server 通用类库
  14. 无法重启oracle数据库监听
  15. nodejs抓取别人家的页面的始末
  16. 我们为何放弃Eclipse,投奔IntelliJ IDEA
  17. sea.js常用接口
  18. jQuery队列(一)
  19. C# 中的 enum(枚举) 类型使用例子
  20. Python核心编程——Chapter15

热门文章

  1. CALayer -- 备忘
  2. iOS之手势滑动返回功能-b
  3. [BZOJ 2048] [2009国家集训队]书堆 【调和级数】
  4. Word图片显示不完整
  5. Python模块subprocess小记
  6. ruby面向对象class
  7. bzoj2125 3047
  8. bzoj2893
  9. EXT 组件一些属性与方法(Tree)
  10. python 正则表达式 贪婪模式的简介和匹配时的几种模式