java实现多线程下载文件

java实现多线程下载文件packagedownload;importjava.io.BufferedReader;importjava.io.File;importja

大家好,欢迎来到IT知识分享网。

package download;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.RandomAccessFile;

import java.net.HttpURLConnection;

import java.net.URL;

public class down {

/**

* @param args

*/

private static int threadCount = 3;

private static int blockSize;

private static String path = “http://mirrors.shu.edu.cn/apache/tomcat/tomcat-7/v7.0.91/bin/apache-tomcat-7.0.91.zip”;

private static int currentRunThreadCount = 0;

public static void main(String[] args) {

try {

URL url = new URL(path);

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

connection.setRequestMethod(“GET”);

connection.setConnectTimeout(100 * 1000);

int code = connection.getResponseCode();

if(code == 200) {

int fileLength = connection.getContentLength();

RandomAccessFile randomAccessFile = new RandomAccessFile(new File(getFileName(path)), “rw”);

randomAccessFile.setLength(fileLength);

blockSize = fileLength / threadCount;

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

int startThread = i * blockSize;

int endThread = (i + 1) * blockSize – 1;

if( i == blockSize – 1) endThread = fileLength -1;

new DownloadThread(i, startThread, endThread).start();

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

public static class DownloadThread extends Thread {

private int threadId;

private int endThread;

private int startThred;

public DownloadThread(int threadId, int startThred, int endThread) {

this.threadId = threadId;

this.startThred = startThred;

this.endThread = endThread;

}

public void run() {

synchronized (DownloadThread.class) {

currentRunThreadCount += 1;

}

//分段请求网络连接,分段保存在本地

try {

System.err.println(“理论线程:”+threadId+”,开始位置:”+startThred+”,结束位置:”+endThread);

URL url = new URL(path);

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

connection.setRequestMethod(“GET”);

connection.setConnectTimeout(100 * 1000);

File file = new File(threadId+”.txt”);

if(file.exists()) { //是否断点

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

String lastPostion_str = bufferedReader.readLine();

startThred = Integer.parseInt(lastPostion_str);

bufferedReader.close();

}

//设置分段下载的头信息 Range:做分段

connection.setRequestProperty(“Range”, “bytes:”+startThred+”-” + endThread); //开始下载点

int code = connection.getResponseCode();

System.out.println(code);

if(code == 200) { //200:请求全部资源成功 206:代表部分资源请求成功

InputStream inputStream = connection.getInputStream();

System.out.println(getFileName(path));

RandomAccessFile randomAccessFile = new RandomAccessFile(new File(getFileName(path)), “rw”);

randomAccessFile.seek(startThred);

byte[] buffer = new byte[1024*10];

int length = -1;

int total = 0;//记录下载的总量

System.err.println(“实际线程:”+threadId+”,开始位置:”+startThred+”,结束位置:”+endThread);

while((length = inputStream.read(buffer)) != -1) {

randomAccessFile.write(buffer, 0, length);

total += length;

int currentThreadPostion = startThred + total;

RandomAccessFile randomAccessFile2 = new RandomAccessFile(file, “rwd”);

randomAccessFile2.write(String.valueOf(currentThreadPostion).getBytes());

randomAccessFile2.close();

}

randomAccessFile.close();

inputStream.close();

System.err.println(“线程:”+threadId+”下载完毕”);

synchronized (DownloadThread.class) {

currentRunThreadCount -= 1;

if(currentRunThreadCount == 0){

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

File file2 = new File(i+”.txt”);

// file2.delete();

}

}

}

}

} catch (Exception e) {

e.printStackTrace();

}

super.run();

}

}

public static String getFileName(String path) {

return path.substring(path.lastIndexOf(“/”)+1);

}

}

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/57448.html

(0)
上一篇 2024-07-11 20:33
下一篇 2024-07-12 15:26

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信