在请求头里设置Range,可以拿到不同的部分,前提还需要web server支持。
/**
* 开始下载
* @throws Exception
*/
public void startDown() throws Exception{
HttpClient httpClient = new DefaultHttpClient()
try {
//获取下载文件信息
getDownloadFileInfo(httpClient)
startDownloadThread()
//开始监视下载数据
monitor()
} catch (Exception e) {
throw e
} finally {
httpClient.getConnectionManager().shutdown()
}
}
/**
* 获取下载文件信息
*/
private void getDownloadFileInfo(HttpClient httpClient) throws IOException,
ClientProtocolException, Exception {
HttpHead httpHead = new HttpHead(url)
HttpResponse response = httpClient.execute(httpHead)
//获取HTTP状态码
int statusCode = response.getStatusLine().getStatusCode()
if(statusCode != 200) throw new Exception("资源不存在!")
if(getDebug()){
for(Header header : response.getAllHeaders()){
System.out.println(header.getName()+":"+header.getValue())
}
}
//Content-Length
Header[] headers = response.getHeaders("Content-Length")
if(headers.length > 0)
contentLength = Long.valueOf(headers[0].getValue())
httpHead.abort()
httpHead = new HttpHead(url)
httpHead.addHeader("Range", "bytes=0-"+(contentLength-1))
response = httpClient.execute(httpHead)
if(response.getStatusLine().getStatusCode() == 206){
acceptRanges = true
}
httpHead.abort()
}
/**
* 启动多个下载线程
* @throws IOException
* @throws FileNotFoundException
*/
private void startDownloadThread() throws IOException,
FileNotFoundException {
//创建下载文件
File file = new File(localPath)
file.createNewFile()
RandomAccessFile raf = new RandomAccessFile(file, "rw")
raf.setLength(contentLength)
raf.close()
//定义下载线程事件实现类
DownloadThreadListener listener = new DownloadThreadListener() {
public void afterPerDown(DownloadThreadEvent event) {
//下载完一个片段后追加已下载字节数
synchronized (object) {
DownloadTask.this.receivedCount += event.getCount()
}
}
public void downCompleted(DownloadThreadEvent event) {
//下载线程执行完毕后从主任务中移除
threads.remove(event.getTarget())
if(getDebug()){
System.out.println("剩余线程数:"+threads.size())
}
}
}
//不支持多线程下载时
if (!acceptRanges) {
if(getDebug()){
System.out.println("该地址不支持多线程下载")
}
//定义普通下载
DownloadThread thread = new DownloadThread(url, 0, contentLength, file, false)
thread.addDownloadListener(listener)
thread.start()
threads.add(thread)
return
}
//每个请求的大小
long perThreadLength = contentLength / threadCount + 1
long startPosition = 0
long endPosition = perThreadLength
//循环创建多个下载线程
do{
if(endPosition >= contentLength)
endPosition = contentLength - 1
DownloadThread thread = new DownloadThread(url, startPosition, endPosition, file)
thread.addDownloadListener(listener)
thread.start()
threads.add(thread)
startPosition = endPosition + 1//此处加 1,从结束位置的下一个地方开始请求
endPosition += perThreadLength
} while (startPosition < contentLength)
}
HttpClient简单使用
准备
Apache官网下载 HttpClient , 下不了的点击这里,下载完后解压取lib文件夹中jar包导入到项目中
在进行本例之前需要了解三个类
HttpClient 代表Http客户端 里面定义了很多http 请求执行行为
HttpEntity 消息载体,发送或者接收消息的载体,可以通过客户端请求或者服务器响应获取实例
HttpConnection 代表http连接
[java] view plaincopyprint?public class HttpCLientDemo
{
// HttpClient 代表Http客户端
// HttpEntity 消息载体,发送或者接收消息的载体,可以通过客户端请求或者服务器响应获取实例
// HttpConnection 代表http连接
/**
* @param args
*/
public static void main(String[] args)
{
// 创建默认的客户端实例
HttpClient httpCLient = new DefaultHttpClient()
// 创建get请求实例
HttpGet httpget = new HttpGet("http://www.baidu.com")
System.out.println("executing request "+httpget.getURI())
try
{
// 客户端执行get请求 返回响应实体
HttpResponse response = httpCLient.execute(httpget)
// 服务器响应状态行
System.out.println(response.getStatusLine())
Header[] heads = response.getAllHeaders()
// 打印所有响应头
for(Header h:heads){
System.out.println(h.getName()+":"+h.getValue())
}
// 获取响应消息实体
HttpEntity entity = response.getEntity()
System.out.println("------------------------------------")
if(entity != null){
//响应内容
System.out.println(EntityUtils.toString(entity))
System.out.println("----------------------------------------")
// 响应内容长度
System.out.println("响应内容长度:"+entity.getContentLength())
}
} catch (ClientProtocolException e){
e.printStackTrace()
} catch (IOException e){
e.printStackTrace()
}finally{
httpCLient.getConnectionManager().shutdown()
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)