使用java编写一个多线程下载器,需要在URL栏中输入网址,然后通过网址下载。该怎么实现,求源代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用java编写一个多线程下载器,需要在URL栏中输入网址,然后通过网址下载。该怎么实现,求源代码相关的知识,希望对你有一定的参考价值。

使用java编写一个下载器,在URL栏中输入网址,然后点击开始按钮开始下载,并在下载进度中使用进度条显示进度,求代码。

swing做前台界面。后台使用java.net中的HTTPConnection下载就OK。下载可以用getInputStream()获取数据,然后写入文件。只提供思路,无代码。不搞java好多年…… 参考技术A 你是北科天院的吧,我是你们于老师,这种题目是我跟几个计算机老师一起商量出的,不是问问就能找到的。你必须做到理解思路,和功能的实现情况,这些到时候我都会问你的。 参考技术B 等我14号做完了给你粘上来,O(∩_∩)O哈哈~ 参考技术C 于老师都发话了,俺们支持她的意见 参考技术D 这...这..于老师都发话了.....我还是保留意见吧....

Java实现多线程下载 URL以及URLConnection

 主线程:

public class MultiThreadDown {

public static void main(String[] args) throws Exception{

//初始化Downutil对象

    final DownUtil downutil = new DownUtil("http://www.crazyit.org/"

+"attachment.PHP?aid=MTY0NXxjNjBIYznjN3wxMzE1NTQ2MjU5fGNho"

+"D1KVmpXVmhpNG1kWmVzR2JZbnluZWpqs11Od3JzckdodXJOMUpOWWt0aTJz,"

, "oracelsql.rar", 4);

//开始下载

downutil.download();

new Thread(){

public void run(){

while(downutil.getCompleteRate() < 1){

//每隔0.1秒查询一次任务完成的进度

//GUI程序中可根据进度来绘制进度条

System.out.println("已完成:" + downutil.getCompleteRate());

try {

Thread.sleep(100);

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

}

}

}.start();

}

}


package nettest;


import Java.io.InputStream;

import java.io.RandomAccessFile;

import java.NET.HttpURLConnection;

import java.Net.URL;


public class DownUtil {

//定义下载资源的路径

private String path;

//指定所下载文件的保存位置

private String targetFile;

//定义需要使用多少个线程下载资源

private int threadNum;

//定义下载的线程对象

private DownThread[] threads;

//定义下载的文件的大小

private int fileSize;

public DownUtil(String path,String targetFile,int threadNum){

this.path = path;

this.targetFile = targetFile;

this.threadNum = threadNum;

threads = new DownThread[threadNum]; 

}

public void download() throws Exception{

URL url = new URL(path);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setConnectTimeout(5*1000);

conn.setRequestMethod("GET");

conn.addRequestProperty(

"Accept", 

"image/gif,image/jpeg,image/pjpeg,image/pjpeg,"

+"application/x-shockwave-flash,application/xaml + xml,"

+"application/vnd.ms-xpsdocument,application/x-ms-xbap,"

+"application/x-ms-application,application/vnd.ms-excel,"

+"application/vnd.ms-powerpoint,application/msword,*/*");

conn.setRequestProperty("Accept-Language", "zh-CN");

conn.setRequestProperty("Charset", "UTF-8");

conn.setRequestProperty("Connection", "Keep-Alive");

fileSize = conn.getContentLength();

conn.disconnect();

int currentPartSize = fileSize/threadNum + 1;

RandomAccessFile file = new RandomAccessFile(targetFile, "rw");

//得到设置本地文件的大小

file.setLength(fileSize);

file.close();

for (int i = 0; i < threadNum; i++) {

//计算每个线程下载的开始位置

int startPos = i*currentPartSize;

//每个线程使用一个RandomAccessFile进行下载

RandomAccessFile currentPart = new RandomAccessFile(targetFile, "rw");

currentPart.seek(startPos);

//创建下载线程

threads[i] = new DownThread(startPos, currentPartSize, currentPart);

//启动下载线程

threads[i].start();

}

}

//获得下载完成的百分比

public double getCompleteRate(){

//统计多个线程已经下载的总大小

int sumSize = 0;

for (int i = 0; i < threadNum; i++) {

sumSize += threads[i].length;

}

//返回已经完成的百分比

return sumSize*1.0/fileSize;

}

private class DownThread extends Thread{

//当前线程的下载位置

private int startPos;

//定义当前线程负责下载的文件大小

private int currentPartSize;

//当前线程需要下载的文件块

private RandomAccessFile currentPart;

//定义该线程已经下载的字节数

public int length;

public DownThread(int startPos,int currentPartSize,RandomAccessFile currentPart){

this.startPos = startPos;

this.currentPartSize = currentPartSize;

this.currentPart = currentPart;

}

public void run(){

try{

URL url = new URL(path);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setConnectTimeout(5*1000);

conn.setRequestMethod("GET");

conn.addRequestProperty(

"Accept", 

"image/gif,image/jpeg,image/pjpeg,image/pjpeg,"

+"application/x-shockwave-flash,application/xaml + xml,"

+"application/vnd.ms-xpsdocument,application/x-ms-xbap,"

+"application/x-ms-application,application/vnd.ms-excel,"

+"application/vnd.ms-powerpoint,application/msword,*/*");

conn.setRequestProperty("Accept-Language", "zh-CN");

conn.setRequestProperty("Charset", "UTF-8");

InputStream inStream = conn.getInputStream();

//跳过startPos个字节,表明该线程只下载自己负责的那部分文件

inStream.skip(this.startPos);

byte[] buffer = new byte[1024];

int hasRead = 0;

while(length < currentPartSize && (hasRead = inStream.read(buffer)) != -1){

currentPart.write(buffer, 0, hasRead);

//累计该线程的下载大小

length += hasRead;

}

currentPart.close();

inStream.close();

}catch (Exception e){

e.printStackTrace();

}

}

}

}

 

以上是关于使用java编写一个多线程下载器,需要在URL栏中输入网址,然后通过网址下载。该怎么实现,求源代码的主要内容,如果未能解决你的问题,请参考以下文章

Java多线程文件下载性能

#私藏项目实操分享#Java多线程下载器项目实例之线程池

#私藏项目实操分享#Java多线程下载器基础代码

Java 多线程下载器的设计与实现

Java多线程下载器FileDownloader(支持断点续传代理等功能)

Java多线程开发注意问题