服務端支持多線程下載
- controller層
/**
* 多線程文件下載
* @param filename
* @return
*/
@GetMapping("/files/{filename:.+}")
@ResponseBody
public ResponseEntity<Resource> serveFile(@PathVariable String filename) {
Resource file = propertiesService.loadAsResource(filename);
return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + file.getFilename() + "\"").body(file);
}
- service層
@Override
public Resource loadAsResource(String filename) {
Path rootLocation = Paths.get(filePath +"/");
//filePath爲我的文件路徑前綴,例如:D:\project
try {
Path file = rootLocation.resolve(filename);
Resource resource = new UrlResource(file.toUri());
if (resource.exists() || resource.isReadable()) {
return resource;
} else {
log.info("Could not read file: " + filename);
}
} catch (MalformedURLException e) {
log.info("Could not read file: " + filename);
}
return null;
}
客戶端下載實現
- 思路
多線程下載不僅需要客戶端的支持,也需要服務端的支持。 conn.setRequestProperty(“Range”, “bytes=” + startIndex + “-” + endIndex);Range響應頭是多線程下載分割的關鍵所在。
下載思路:首先判斷下載文件大小,配合多線程分割定製http請求數量和請求內容,響應到寫入到RandomAccessFile指定位置中。在俗點就是大的http分割成一個個小的http請求,相當於每次請求一個網頁。RandomAccessFile文件隨機類,可以向文件寫入指定位置的流信息。
- 啓動入口
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
/**
* <p>
* 啓動入口
* </p>
*
* @author algerfan
* @since 2019/6/9 17
*/
public class DownloadMain {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入下載文件的地址,按ENTER結束");
String downloadPath = scanner.nextLine();
System.out.println("下載的文件名及路徑爲:" + MultiPartDownLoad.downLoad(downloadPath));
try {
System.out.println("下載完成,本窗口5s之後自動關閉");
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.exit(0);
}
}
- 客戶端線程池Constans類
import java.util.concurrent.*;
/**
* <p>
* 客戶端線程池Constans類
* </p>
*
* @author algerfan
* @since 2019/6/9 17
*/
public class Constans {
public static final int MAX_THREAD_COUNT = getSystemProcessCount();
private static final int MAX_IMUMPOOLSIZE = MAX_THREAD_COUNT;
/**
* 自定義線程池
*/
private static ExecutorService MY_THREAD_POOL;
/**
* 自定義線程池
*/
public static ExecutorService getMyThreadPool(){
if(MY_THREAD_POOL == null) {
MY_THREAD_POOL = Executors.newFixedThreadPool(MAX_IMUMPOOLSIZE);
}
return MY_THREAD_POOL;
}
/**
* 線程池
*/
private static ThreadPoolExecutor threadPool;
/**
* 單例,單任務 線程池
* @return
*/
public static ThreadPoolExecutor getThreadPool(){
if(threadPool == null){
threadPool = new ThreadPoolExecutor(MAX_IMUMPOOLSIZE, MAX_IMUMPOOLSIZE, 3, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(16),
new ThreadPoolExecutor.CallerRunsPolicy()
);
}
return threadPool;
}
/**
* 獲取服務器cpu核數
* @return
*/
private static int getSystemProcessCount(){
return Runtime.getRuntime().availableProcessors();
}
}
- 客戶端多線程下載類MultiPartDownLoad
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.locks.ReentrantLock;
/**
* <p>
* 客戶端多線程下載類
* </p>
*
* @author algerfan
* @since 2019/6/9 17
*/
public class MultiPartDownLoad {
/**
* 線程下載成功標誌
*/
private static int flag = 0;
/**
* 服務器請求路徑
*/
private String serverPath;
/**
* 本地路徑
*/
private String localPath;
/**
* 線程計數同步輔助
*/
private CountDownLatch latch;
/**
* 定長線程池
*/
private static ExecutorService threadPool;
public MultiPartDownLoad(String serverPath, String localPath) {
this.serverPath = serverPath;
this.localPath = localPath;
}
public boolean executeDownLoad() {
try {
URL url = new URL(serverPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//設置超時時間
conn.setConnectTimeout(5000);
//設置請求方式
conn.setRequestMethod("GET");
conn.setRequestProperty("Connection", "Keep-Alive");
int code = conn.getResponseCode();
if (code != 200) {
System.out.println(String.format("無效網絡地址:%s", serverPath));
return false;
}
//服務器返回的數據的長度,實際上就是文件的長度,單位是字節
// int length = conn.getContentLength(); //文件超過2G會有問題
long length = getRemoteFileSize(serverPath);
System.out.println("文件總長度:" + length + "字節(B)");
RandomAccessFile raf = new RandomAccessFile(localPath, "rwd");
//指定創建的文件的長度
raf.setLength(length);
raf.close();
//分割文件
int partCount = Constans.MAX_THREAD_COUNT;
int partSize = (int)(length / partCount);
latch = new CountDownLatch(partCount);
threadPool = Constans.getMyThreadPool();
for (int threadId = 1; threadId <= partCount; threadId++) {
// 每一個線程下載的開始位置
long startIndex = (threadId - 1) * partSize;
// 每一個線程下載的開始位置
long endIndex = startIndex + partSize - 1;
if (threadId == partCount) {
//最後一個線程下載的長度稍微長一點
endIndex = length;
}
System.out.println("線程" + threadId + "下載:" + startIndex + "字節~" + endIndex + "字節");
threadPool.execute(new DownLoadThread(threadId, startIndex, endIndex, latch));
}
latch.await();
if(flag == 0){
return true;
}
} catch (Exception e) {
System.out.println("文件下載失敗");
}
return false;
}
/**
* 內部類用於實現下載
*/
public class DownLoadThread implements Runnable {
/**
* 線程ID
*/
private int threadId;
/**
* 下載起始位置
*/
private long startIndex;
/**
* 下載結束位置
*/
private long endIndex;
private CountDownLatch latch;
DownLoadThread(int threadId, long startIndex, long endIndex, CountDownLatch latch) {
this.threadId = threadId;
this.startIndex = startIndex;
this.endIndex = endIndex;
this.latch = latch;
}
@Override
public void run() {
try {
System.out.println("線程" + threadId + "正在下載...");
URL url = new URL(serverPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestMethod("GET");
//請求服務器下載部分的文件的指定位置
conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex);
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
System.out.println("線程" + threadId + "請求返回code=" + code);
//返回資源
InputStream is = conn.getInputStream();
RandomAccessFile raf = new RandomAccessFile(localPath, "rwd");
//隨機寫文件的時候從哪個位置開始寫
//定位文件
raf.seek(startIndex);
int len;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
raf.write(buffer, 0, len);
}
is.close();
raf.close();
System.out.println("線程" + threadId + "下載完畢");
} catch (Exception e) {
//線程下載出錯
MultiPartDownLoad.flag = 1;
System.out.println("線程下載出錯");
} finally {
//計數值減一
latch.countDown();
}
}
}
/**
* 內部方法,獲取遠程文件大小
* @param remoteFileUrl
* @return
* @throws IOException
*/
private long getRemoteFileSize(String remoteFileUrl) throws IOException {
long fileSize;
HttpURLConnection httpConnection = (HttpURLConnection) new URL(remoteFileUrl).openConnection();
httpConnection.setRequestMethod("HEAD");
int responseCode = 0;
try {
responseCode = httpConnection.getResponseCode();
} catch (IOException e) {
e.printStackTrace();
}
if (responseCode >= 400) {
System.out.println("Web服務器響應錯誤!");
return 0;
}
String sHeader;
for (int i = 1;; i++) {
sHeader = httpConnection.getHeaderFieldKey(i);
if ("Content-Length".equals(sHeader)) {
fileSize = Long.parseLong(httpConnection.getHeaderField(sHeader));
break;
}
}
return fileSize;
}
/**
* 下載文件執行器
* @param serverPath
* @return
*/
public synchronized static String downLoad(String serverPath) {
ReentrantLock lock = new ReentrantLock();
lock.lock();
String[] names = serverPath.split("\\.");
if (names.length <= 0) {
return null;
}
String fileTypeName = names[names.length - 1];
String localPath = String.format("/%s.%s", UUID.randomUUID(),fileTypeName);
MultiPartDownLoad m = new MultiPartDownLoad(serverPath, localPath);
long startTime = System.currentTimeMillis();
boolean flag = false;
try{
flag = m.executeDownLoad();
long endTime = System.currentTimeMillis();
if(flag){
System.out.println("文件下載結束,共耗時" + (endTime - startTime)+ "ms");
return localPath;
}
System.out.println("文件下載失敗");
return null;
}catch (Exception ex){
System.out.println("文件下載失敗");
return null;
}finally {
// 重置 下載狀態
MultiPartDownLoad.flag = 0;
if(!flag){
File file = new File(localPath);
file.delete();
}
lock.unlock();
}
}
}