HttpClient 4 实现文件下载
2014-03-08 16:30
393 查看
http://llying.iteye.com/blog/486989
如果大家对WEB研发有兴趣
可以加入Q群:46176507 共同进步学习
有时需要通过httpclient进行文件下载,下面是文件下载的实现过程
Java代码
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class SearchDomain {
public static void main(String[] args) throws ClientProtocolException, IOException {
//实例化一个HttpClient
HttpClient httpClient = new DefaultHttpClient();
//设定目标站点 web的默认端口80可以不写的 当然如果是其它端口就要标明
HttpHost httpHost = new HttpHost("www.shanhe114.com",80);
//设置需要下载的文件
HttpGet httpGet = new HttpGet("/test.zip");
//这里也可以直接使用httpGet的绝对地址,当然如果不是具体地址不要忘记/结尾
//HttpGet httpGet = new HttpGet("http://www.0431.la/");
//HttpResponse response = httpClient.execute(httpGet);
HttpResponse response = httpClient.execute(httpHost, httpGet);
if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){
//请求成功
//取得请求内容
HttpEntity entity = response.getEntity();
//显示内容
if (entity != null) {
//这里可以得到文件的类型 如image/jpg /zip /tiff 等等 但是发现并不是十分有效,有时明明后缀是.rar但是取到的是null,这点特别说明
System.out.println(entity.getContentType());
//可以判断是否是文件数据流
System.out.println(entity.isStreaming());
//设置本地保存的文件
File storeFile = new File("c:/0431la.zip");
FileOutputStream output = new FileOutputStream(storeFile);
//得到网络资源并写入文件
InputStream input = entity.getContent();
byte b[] = new byte[1024];
int j = 0;
while( (j = input.read(b))!=-1){
output.write(b,0,j);
}
output.flush();
output.close();
}
if (entity != null) {
entity.consumeContent();
}
}
}
}
需要说明这里我并没验证文件的类型,实际操作中可以通过url的后缀提取 或者ContentType的类型
如果大家对WEB研发有兴趣
可以加入Q群:46176507 共同进步学习
有时需要通过httpclient进行文件下载,下面是文件下载的实现过程
Java代码
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class SearchDomain {
public static void main(String[] args) throws ClientProtocolException, IOException {
//实例化一个HttpClient
HttpClient httpClient = new DefaultHttpClient();
//设定目标站点 web的默认端口80可以不写的 当然如果是其它端口就要标明
HttpHost httpHost = new HttpHost("www.shanhe114.com",80);
//设置需要下载的文件
HttpGet httpGet = new HttpGet("/test.zip");
//这里也可以直接使用httpGet的绝对地址,当然如果不是具体地址不要忘记/结尾
//HttpGet httpGet = new HttpGet("http://www.0431.la/");
//HttpResponse response = httpClient.execute(httpGet);
HttpResponse response = httpClient.execute(httpHost, httpGet);
if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){
//请求成功
//取得请求内容
HttpEntity entity = response.getEntity();
//显示内容
if (entity != null) {
//这里可以得到文件的类型 如image/jpg /zip /tiff 等等 但是发现并不是十分有效,有时明明后缀是.rar但是取到的是null,这点特别说明
System.out.println(entity.getContentType());
//可以判断是否是文件数据流
System.out.println(entity.isStreaming());
//设置本地保存的文件
File storeFile = new File("c:/0431la.zip");
FileOutputStream output = new FileOutputStream(storeFile);
//得到网络资源并写入文件
InputStream input = entity.getContent();
byte b[] = new byte[1024];
int j = 0;
while( (j = input.read(b))!=-1){
output.write(b,0,j);
}
output.flush();
output.close();
}
if (entity != null) {
entity.consumeContent();
}
}
}
}
需要说明这里我并没验证文件的类型,实际操作中可以通过url的后缀提取 或者ContentType的类型
相关文章推荐
- AsyncHttpClient+Servlet实现文件上传下载
- HttpClient 4.5版本实现文件上传下载以及常用post、get请求
- org.apache.tools.zip.*和org.apache.commons.httpclient.*实现远程文件打包下载,支持中文文件名(转)
- android 使用AsyncHttpClient框架上传文件以及使用HttpURLConnection下载文件
- 第三页(客户端) :远程资源管理器 c#应用源代码,SERVICE + CLIENT 模式 可实现远程文件管理,下载功能
- asphttprequest 实现文件下载, 监听下载进度以及当前网络的状态(WXHL)
- 用vb开发实现http文件下载的ActiveX控件
- java用httpclient 4.3 实现将base64串进行文件上传
- Android Http实现文件的上传和下载
- WebRequest之HttpWebRequest实现服务器上文件的下载(一)
- 实现各种文件的下载方法:用于 WEB上的报表导出(jxl 实现Excel报表下载),核心思想就是将文件的输出流传给HttpResponseOutputStream 里就成了。
- 【java】HttpClient实现HTTP文件通用下载工具类
- Httpclient 实现带参文件上传
- HttpClient文件上传下载
- Java实现HTTP文件下载
- 使用java实现http多线程断点下载文件(一)
- Response对象、HttpServletResponse简介、处理字节编码问题、实现动态文件下载
- day day up--Java实现http文件下载
- HTTP文件下载始末及简单实现
- 发一个多线程通过 HTTP 下载文件的类(Linux下的实现)