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

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

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

copyright paper51.com

图7 新建下载任务对话框

内容来自www.paper51.com

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

内容来自www.paper51.com

paper51.com

图8 浏览对话框 copyright paper51.com

以下是对话框的设计实现核心代码:

paper51.com

protected ControlcreateDialogArea(Composite parent) { 内容来自www.paper51.com

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

内容来自论文无忧网 www.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);

paper51.com

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

内容来自www.paper51.com

new Label(panel, SWT.RIGHT).setText("存储目录:"); 内容来自论文无忧网 www.paper51.com

btnBrowse.setText("浏览"); 内容来自论文无忧网 www.paper51.com

btnBrowse.addSelectionListener(new SelectionAdapter(){

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

public void widgetSelected(SelectionEvent e){ http://www.paper51.com

final Shell shell = Display.getCurrent().getActiveShell(); http://www.paper51.com

DirectoryDialog dlg = new DirectoryDialog(shell);

内容来自www.paper51.com

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

copyright paper51.com

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

dlg.setFilterPath("c:/download"); 内容来自www.paper51.com

String dir = dlg.open(); http://www.paper51.com

if (dir != null) { paper51.com

NewTaskDialog.this.cmbDestFolders.add(dir);

copyright paper51.com

NewTaskDialog.this.cmbDestFolders.select(0);

paper51.com

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

}

内容来自www.paper51.com

});

paper51.com

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

内容来自www.paper51.com

cmbThreadCounts = new Combo(panel, SWT.READ_ONLY);

copyright paper51.com

return panel;}

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

protected voidcreateButtonsForButtonBar(Composite parent) { http://www.paper51.com

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

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

}

http://www.paper51.com

下载功能的实现主要原理是: 内容来自论文无忧网 www.paper51.com

l       判断NewHttpTask构造函数中参数是否合法; copyright paper51.com

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

http://www.paper51.com

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

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

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

paper51.com

l       根据下载线程数把文件分块; paper51.com

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

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

核心代码如下: copyright paper51.com

//建立连接

paper51.com

MultiThreadedHttpConnectionManagerconnectionManager =

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

new MultiThreadedHttpConnectionManager(); paper51.com

connectionManager.getParams().setDefaultMaxConnectionsPerHost(20); copyright paper51.com

httpClient = new HttpClient(connectionManager); paper51.com

//得到文件大小 http://www.paper51.com

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

内容来自www.paper51.com

head.setFollowRedirects(false); 内容来自www.paper51.com

int statusCode = httpClient.executeMethod(head); copyright paper51.com

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

if (statusCode ==HttpStatus.SC_MOVED_TEMPORARILY){

http://www.paper51.com

String recentUrl =head.getResponseHeader("Location").getValue(); paper51.com

logger.info("URL redirection: " +recentUrl); 内容来自www.paper51.com

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

http://www.paper51.com

url.getHost(),

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

url.getPort(),

paper51.com

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

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

copyright paper51.com

statusCode =httpClient.executeMethod(head);

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

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

contentLength = getResourceContentLen();

http://www.paper51.com

msg = "Resource Content Length is " +contentLength;

http://www.paper51.com

logger.info(msg); http://www.paper51.com

listener.onProgress(msg); copyright paper51.com

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

内容来自www.paper51.com

long chunk = contentLength / threadCounts;

http://www.paper51.com

long startPos = 0;

http://www.paper51.com

long endPos; 内容来自www.paper51.com

for (int i = 0; i < threadCounts; i++)

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

{ndPos = startPos + chunk; paper51.com

if (i == threadCounts - 1)

copyright paper51.com

{endPos = contentLength;} 内容来自论文无忧网 www.paper51.com

workers[i] = new HttpTaskWorker( paper51.com

"Worker Thread #" + (i + 1),

copyright paper51.com

startPos, endPos - 1); 内容来自论文无忧网 www.paper51.com

workers[i].start();

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

startPos = endPos;

paper51.com

//存储文件 paper51.com

destination = new File(destination, http://www.paper51.com

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

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

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

protected String getClipBoardURL() {

http://www.paper51.com

Clipboard clipboard = newClipboard(Display.getCurrent());

http://www.paper51.com

TextTransfer textTransfer =TextTransfer.getInstance(); 内容来自www.paper51.com

String textData =(String)clipboard.getContents(textTransfer);

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

URL url; http://www.paper51.com

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

url = new URL(textData); paper51.com

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

return textData;} 内容来自论文无忧网 www.paper51.com

catch(MalformedURLException e) {

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

} paper51.com

return "";

paper51.com

}

copyright paper51.com

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