用apache的httpclient发请求和接受数据
此处发请求的是用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发请求和接受数据的更多相关文章
- PHP通过curl向其它服务器发请求并返回数据
在很多时候,我们都需要请求第三方的服务器来获取一些数据,比如token,比如百度的主动推送,那么我们的php如何实现向第三方服务器发请求呢?我们可以通过curl来实现 首先定义请求的url,然后创建h ...
- Ajax向Controller发送请求并接受数据需要注意的一个细节
想用Ajax想向Controller发送请求和接收返回的字符等等.Controller中要使用@ResponseBody注解. <script type="text/javascrip ...
- java apache commons HttpClient发送get和post请求的学习整理(转)
文章转自:http://blog.csdn.net/ambitiontan/archive/2006/01/06/572171.aspx HttpClient 是我最近想研究的东西,以前想过的一些应用 ...
- httpClient模拟浏览器发请求
一.介绍 httpClient是Apache公司的一个子项目, 用来提高高效的.最新的.功能丰富的支持http协议的客户端编程工具包.完成可以模拟浏览器发起请求行为. 二.简单使用例子 : 模拟浏览器 ...
- java模拟http请求上传文件,基于Apache的httpclient
1.依赖 模拟http端的请求需要依赖Apache的httpclient,需要第三方JSON支持,项目中添加 <dependency> <groupId>org.apache& ...
- Java使用Apache的HttpClient组件发送https请求
如果我们直接通过普通的方式对https的链接发送请求,会报一个如下的错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.Va ...
- 使用HttpClient实现对第三方服务器的请求并接受返回数据
/* * 创建日期 2017-4-7 * * TODO 要更改此生成的文件的模板,请转至 * 窗口 - 首选项 - Java - 代码样式 - 代码模板 */ package com.enfo.int ...
- 使用apache的HttpClient进行http通讯,隐藏的HTTP请求头部字段是如何自动被添加的
我们用apache的HttpClient这个库消费云端的Restful API时,一般都需要两次HTTP调用,第一次获得某种token,比如获取防止跨域请求伪造攻击Cross-site request ...
- Java HttpClient伪造请求之简易封装满足HTTP以及HTTPS请求
HttpClient简介 HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 jav ...
随机推荐
- ssh登录docker容器
ssh登录docker容器1.启动一个docker容器# docker run -t -i ubuntu/ruby:v1 /bin/bash2.然后在容器里,安装openssh-server open ...
- 推荐几款jQuery表格插件
平时项目中,会碰到很多表格元素,这里推荐几款jQuery表格插件. Stackable.js 通常在小屏幕上,表格的表形形式不大好,因为用户会缩放平移,或者就是表格太小,导致数据不可见.Stackab ...
- 【Xamarin挖墙脚系列:Xamarin的核心】
原文:[Xamarin挖墙脚系列:Xamarin的核心] Xamarin 包含两个商业产品 :Xamarin.IOS, Xamarin.Android.他们都是通过开源的基于.Net的Mono项目构建 ...
- 启动redis出现Creating Server TCP listening socket *:6379: bind: No such file or directory
E:\redis>redis-server.exe redis.windows.conf [8564] 10 Oct 20:00:36.745 # Creating Server TCP lis ...
- 17.1 Replication Configuration 复制配置
17.1 Replication Configuration 复制配置 17.1.1 How to Set Up Replication 17.1.2 Replication Formats 17.1 ...
- 【HDOJ】2159 FATE
DP+贪心优化. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXNUM ...
- String 类;Math 类;
static void Main(string[] args) { while (true) { /* strin ...
- 还能输入多少字?(JS动态计算)
<div class="m-form ovh"> <div class="hd"> <span class="fr&qu ...
- 算法 python实现(一) 基本常识
我算法和数据结构都不好,笨的一比. 现在的目标是熟悉常见和经典算法,看本站两个大牛的博客,在这也推荐一下,特别好,除了算法其他的技术也很不错. Vamei : http://www.cnblogs.c ...
- NodeAsp——像开发NodeJS应用一样玩转ASP
NodeAsp是一套Classic ASP框架,借鉴了NodeJS的模块化思想,让您可以使用全新的理念愉快地书写ASP程序. NodeAsp使用遵循CommonJS规范的require,完全兼容Nod ...