论文无忧网提供:计算机毕业论文范文|计算机毕业设计|计算机毕业论文
栏目导航 ASP Java Web .NET VB6.0 JAVA VC VF DELPHI PB 计算机网络 计算机科学与技术 PHP 安卓APP 其他 C# 代写论文
当前位置: > 计算机 > 计算机科学与技术 >

java多线程与线程安全实践-基于http协议的断点续传(论文+程序)

新建下载任务对话框的设计主要是根据JFace中Dialog设计实现,如图7所示:

http://www.paper51.com

copyright paper51.com

图7 新建下载任务对话框 内容来自论文无忧网 www.paper51.com

从对话框中可以看到下载任务的三个主要参数:URL地址、存储目录、以及选择下载线程数,在信息填写完毕后按下确定按钮就会对NewHttpTask建立实例开始进行下载。同时在点击浏览按钮时会弹出浏览目录对话框,如图8所示:

copyright paper51.com

paper51.com

图8 浏览对话框

内容来自论文无忧网 www.paper51.com

以下是对话框的设计实现核心代码: http://www.paper51.com

protected ControlcreateDialogArea(Composite parent) { copyright paper51.com

Composite composite = (Composite)super.createDialogArea(parent); paper51.com

Composite panel = new Composite(composite, SWT.None);

copyright paper51.com

GridData panelData = new GridData(SWT.FILL, SWT.FILL, true, true); copyright paper51.com

panel.setLayoutData(panelData);

http://www.paper51.com

panel.setLayout(new GridLayout(5, false));

paper51.com

new Label(panel, SWT.RIGHT).setText("存储目录:");

内容来自www.paper51.com

btnBrowse.setText("浏览");

copyright paper51.com

btnBrowse.addSelectionListener(new SelectionAdapter(){

paper51.com

public void widgetSelected(SelectionEvent e){

内容来自www.paper51.com

final Shell shell = Display.getCurrent().getActiveShell(); paper51.com

DirectoryDialog dlg = new DirectoryDialog(shell); copyright paper51.com

dlg.setText("浏览文件夹");

内容来自www.paper51.com

dlg.setMessage("请选择一个目录:");

paper51.com

dlg.setFilterPath("c:/download"); paper51.com

String dir = dlg.open(); 内容来自www.paper51.com

if (dir != null) {

内容来自www.paper51.com

NewTaskDialog.this.cmbDestFolders.add(dir); paper51.com

NewTaskDialog.this.cmbDestFolders.select(0);

paper51.com

}

copyright paper51.com

} paper51.com

}); paper51.com

new Label(panel,SWT.NONE).setText("选择下载线程数"); copyright paper51.com

cmbThreadCounts = new Combo(panel, SWT.READ_ONLY); http://www.paper51.com

return panel;}

copyright paper51.com

protected voidcreateButtonsForButtonBar(Composite parent) {

paper51.com

createButton(parent, IDialogConstants.OK_ID, "确定", true);

copyright paper51.com

createButton(parent, IDialogConstants.CANCEL_ID, "取消", false); http://www.paper51.com

} copyright paper51.com

下载功能的实现主要原理是: paper51.com

l       判断NewHttpTask构造函数中参数是否合法;

paper51.com

l       然后通过使用HttpClient包,建立HTTP连接; paper51.com

l       通过Head方法得到相应文件的长度信息; 内容来自www.paper51.com

l       判断是否支持多线程下载; 内容来自论文无忧网 www.paper51.com

l       通过判断状态码(Status Code)判断是否有异常或者文件是全部下载还是部分下载,也就是文件是第一次下载还是有过停止后继续下载。

http://www.paper51.com

l       根据下载线程数把文件分块; 内容来自论文无忧网 www.paper51.com

l       建立相应线程进行下载,并读入;

内容来自www.paper51.com

l       将读到的数据写到指定路径;

paper51.com

核心代码如下:

内容来自论文无忧网 www.paper51.com

//建立连接 copyright paper51.com

MultiThreadedHttpConnectionManagerconnectionManager = copyright paper51.com

new MultiThreadedHttpConnectionManager();

http://www.paper51.com

connectionManager.getParams().setDefaultMaxConnectionsPerHost(20); http://www.paper51.com

httpClient = new HttpClient(connectionManager);

http://www.paper51.com

//得到文件大小

内容来自论文无忧网 www.paper51.com

HeadMethod head = new HeadMethod(url.toString());

内容来自论文无忧网 www.paper51.com

head.setFollowRedirects(false);

paper51.com

int statusCode = httpClient.executeMethod(head); 内容来自论文无忧网 www.paper51.com

//判断资源是否支持多线程下载 paper51.com

if (statusCode ==HttpStatus.SC_MOVED_TEMPORARILY){ 内容来自www.paper51.com

String recentUrl =head.getResponseHeader("Location").getValue(); 内容来自www.paper51.com

logger.info("URL redirection: " +recentUrl);

内容来自论文无忧网 www.paper51.com

this.url = new URL(url.getProtocol(),

paper51.com

url.getHost(),

paper51.com

url.getPort(), 内容来自www.paper51.com

recentUrl);

paper51.com

head = new HeadMethod(url.toString());

内容来自www.paper51.com

statusCode =httpClient.executeMethod(head); 内容来自www.paper51.com

//将文件分块,并建立相应线程完成下载

copyright paper51.com

contentLength = getResourceContentLen();

内容来自论文无忧网 www.paper51.com

msg = "Resource Content Length is " +contentLength; 内容来自www.paper51.com

logger.info(msg); 内容来自www.paper51.com

listener.onProgress(msg);

paper51.com

listener.onGetContentLength(destination.getName(),contentLength);

http://www.paper51.com

long chunk = contentLength / threadCounts;

http://www.paper51.com

long startPos = 0;

内容来自论文无忧网 www.paper51.com

long endPos; paper51.com

for (int i = 0; i < threadCounts; i++) copyright paper51.com

{ndPos = startPos + chunk; http://www.paper51.com

if (i == threadCounts - 1) http://www.paper51.com

{endPos = contentLength;} http://www.paper51.com

workers[i] = new HttpTaskWorker( 内容来自论文无忧网 www.paper51.com

"Worker Thread #" + (i + 1), http://www.paper51.com

startPos, endPos - 1); 内容来自www.paper51.com

workers[i].start();

copyright paper51.com

startPos = endPos; copyright paper51.com

//存储文件

内容来自www.paper51.com

destination = new File(destination, paper51.com

new File(url.getPath()).getName());

内容来自论文无忧网 www.paper51.com

store = new RandomAccessFile(destination,"rw"); http://www.paper51.com

另外,由于在新建任务栏中需要填入下载资源地址URL,而URL地址经常是一串很长的字符串,每次都手工复制、粘贴到下载软件任务栏中显然非常麻烦。为了用户使用更加的方便,下载软件在新建任务时支持将系统剪贴板中的内容(URL地址)自动添加到新建任务中的URL地址栏中,这样就意味着用户在粘贴地址后再打开新建下载任务时,ULR地址已经被填在新建任务中的URL地址栏。这样可以大大节省用户的工作量。主要代码如下:

copyright paper51.com

protected String getClipBoardURL() { paper51.com

Clipboard clipboard = newClipboard(Display.getCurrent()); copyright paper51.com

TextTransfer textTransfer =TextTransfer.getInstance();

http://www.paper51.com

String textData =(String)clipboard.getContents(textTransfer); 内容来自论文无忧网 www.paper51.com

URL url;

http://www.paper51.com

try { http://www.paper51.com

url = new URL(textData); 内容来自www.paper51.com

if(url.getProtocol().equals("http"))

http://www.paper51.com

return textData;} paper51.com

catch(MalformedURLException e) {

内容来自www.paper51.com

}

paper51.com

return "";

http://www.paper51.com

} http://www.paper51.com

------分隔线----------------------------
联系方式