/**
*Author: Jiangtao He; Email: [email protected]
*File Name: DownloadThread.java
*Date: 2012-1-18
*Copyright: All right reserved by author - Jiangtao He
*Version: MyJavaExpert v1.0
*/
package com.ross.httpdownload;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.Date;
import com.ross.httpdownload.util.*;
/**
* Author: Jiangtao He; Email: [email protected]
* Date: 2012-1-18
* Since: MyJavaExpert v1.0
* Description: download thread implementation.
*/
public class DownloadThread implements Runnable
{
private String sFileURL;
private long lStartIndex;
private long lEndIndex;
private int iThreadId;
private String sFullFileName;
private HttpProcess oHttpProcess;
/**
* Description: default constructor, it is used for initializing the fields.
*/
public DownloadThread()
{
oHttpProcess = new HttpProcess();
}
/**
* Author: Jiangtao He; Email: [email protected]
* Description: it will implement the download logic
*/
public void run()
{
System.out.println("The download thread "
+ this.iThreadId
+ " is started at "
+ (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
.format(new Date()));
System.out.println("The download index of " + this.iThreadId + " is "
+ this.lStartIndex + " to " + this.lEndIndex);
// content length
long lContentlen = 0;
// used to store the new url connection
URLConnection oURLCon = null;
// used to store the input stream of the response
BufferedInputStream oBIn = null;
// used to store the output writer
RandomAccessFile oRAFile = null;
// create the buffer
byte[] bBuffer = new byte[SysValue.Buffer_Size];
// get URL connection
try
{
// create a link for each thread
oURLCon = oHttpProcess.getURLConnection(this.sFileURL);
// allow the user interaction, for example: a verify pop window
oURLCon.setAllowUserInteraction(true);
// set the resource range
oURLCon.setRequestProperty("Range", "bytes=" + this.lStartIndex
+ "-" + this.lEndIndex);
// get the url connection input stream
oBIn = new BufferedInputStream(oURLCon.getInputStream());
// initialize the random access file object
oRAFile = new RandomAccessFile(this.sFullFileName, "rw");
oRAFile.seek(this.lStartIndex);
// read the stream from http connection
int iLen = 0;
int iActualLen = 0;
while (iActualLen < (this.lEndIndex - this.lStartIndex + 1))
{
iLen = oBIn.read(bBuffer, 0, SysValue.Buffer_Size);
if (-1 == iLen)
{
break;
}
// write the read data to file
oRAFile.write(bBuffer, 0, iLen);
// move the position mark
iActualLen = iActualLen + iLen;
}
System.out.println("Thread " + this.iThreadId
+ " download is finished, totaly: " + iActualLen);
}
catch (IOException e)
{
System.out.println("download the file failed, IO Exception"
+ e.getMessage());
e.printStackTrace();
}
finally
{
if (null != oRAFile)
{
try
{
oRAFile.close();
}
catch (IOException e)
{
System.out
.println("close object of RandomAccessFile failed, IO Exception"
+ e.getStackTrace());
}
}
if (null != oBIn)
{
try
{
oBIn.close();
}
catch (IOException e)
{
System.out
.println("close input stream of http connection failed, IO Exception"
+ e.getStackTrace());
}
}
}
System.out.println("The download thread "
+ this.iThreadId
+ " is ended at "
+ (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
.format(new Date()));
}
public String getSFileURL()
{
return sFileURL;
}
public void setSFileURL(String fileURL)
{
sFileURL = fileURL;
}
public long getLStartIndex()
{
return lStartIndex;
}
public void setLStartIndex(long startIndex)
{
lStartIndex = startIndex;
}
public long getLEndIndex()
{
return lEndIndex;
}
public void setLEndIndex(long endIndex)
{
lEndIndex = endIndex;
}
public int getIThreadId()
{
return iThreadId;
}
public void setIThreadId(int threadId)
{
iThreadId = threadId;
}
public String getSFullFileName()
{
return sFullFileName;
}
public void setSFullFileName(String fullFileName)
{
sFullFileName = fullFileName;
}
}

~诸行无常~
- 粉丝: 11
最新资源
- 卷烟工业控制网络安全技术的研究应用.docx
- 中小学生网络写作问题探究.docx
- 单片机智能小车研究与设计开发书.doc
- 2018届高三数学一轮复习-第十二章-复数、算法、推理与证明-第三节-合情推理与演绎推理-理.ppt
- 利用web技术构建语音多媒体在线练习与测试平台研究与应用申报书.doc
- 对搜索引擎竞价排名引发的商标侵权纠纷的思考.docx
- (源码)基于Java的Flappy Bird游戏.zip
- 第二章-微型计算机结构.ppt
- 软件工程项目之项目计划与质量管理概述.doc
- 疾病预防控制中心档案信息化建设探究.docx
- 网络教学系统在高校计算机教学中的应用方法研究.docx
- 员工费用支出、报销、保险待遇理赔记录表(Excel表格通用模板).xls
- PLC课程设计(电机清洗机).doc
- 学校管理团队信息化领导力模型的构建.pptx
- 电子商务课程研究设计问卷调查.doc
- 基于 PaddleHub 动物识别模型与百度百科的动物百科 AI 老师
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



- 1
- 2
- 3
- 4
- 5
- 6
前往页