- package com.XXX;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.RandomAccessFile;
- import java.net.HttpURLConnection;
- import java.net.URL;
- public class MultipartThreadDownloador
- {
- /**
- * 需要下載資源的地址
- */
- private String urlStr;
- /**
- * 下載的文件
- */
- private File localFile;
- /**
- * 需要下載文件的存放的本地文件夾路徑
- */
- private String dirStr;
- /**
- * 存儲到本地的文件名
- */
- private String filename;
- /**
- * 開啓的線程數量
- */
- private int threadCount;
- /**
- * 下載文件的大小
- */
- private long fileSize;
- public MultipartThreadDownloador(String urlStr, String dirStr,
- String filename, int threadCount)
- {
- this.urlStr = urlStr;
- this.dirStr = dirStr;
- this.filename = filename;
- this.threadCount = threadCount;
- }
- public void download() throws IOException
- {
- createFileByUrl();
- /**
- * 計算每個線程需要下載的數據長度
- */
- long block = fileSize % threadCount == 0 ? fileSize / threadCount
- : fileSize / threadCount + 1;
- for (int i = 0; i < threadCount; i++)
- {
- long start = i * block;
- long end = start + block >= fileSize ? fileSize : start + block - 1;
- new DownloadThread(new URL(urlStr), localFile, start, end).start();
- }
- }
- /**
- * 根據資源的URL獲取資源的大小,以及在本地創建文件
- */
- public void createFileByUrl() throws IOException
- {
- URL url = new URL(urlStr);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setConnectTimeout(15 * 1000);
- conn.setRequestMethod("GET");
- conn.setRequestProperty(
- "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("Referer", urlStr);
- conn.setRequestProperty("Charset", "UTF-8");
- conn.setRequestProperty(
- "User-Agent",
- "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
- conn.setRequestProperty("Connection", "Keep-Alive");
- conn.connect();
- if (conn.getResponseCode() == 200)
- {
- this.fileSize = conn.getContentLength();// 根據響應獲取文件大小
- if (fileSize <= 0)
- throw new RuntimeException(
- "the file that you download has a wrong size ... ");
- File dir = new File(dirStr);
- if (!dir.exists())
- dir.mkdirs();
- this.localFile = new File(dir, filename);
- RandomAccessFile raf = new RandomAccessFile(this.localFile, "rw");
- raf.setLength(fileSize);
- raf.close();
- System.out.println("需要下載的文件大小爲 :" + this.fileSize + " , 存儲位置爲: "
- + dirStr + "/" + filename);
- } else
- {
- throw new RuntimeException("url that you conneted has error ...");
- }
- }
- private class DownloadThread extends Thread
- {
- /**
- * 下載文件的URI
- */
- private URL url;
- /**
- * 存的本地路徑
- */
- private File localFile;
- /**
- * 是否結束
- */
- private boolean isFinish;
- /**
- * 開始的位置
- */
- private Long startPos;
- /**
- * 結束位置
- */
- private Long endPos;
- public DownloadThread(URL url, File savefile, Long startPos, Long endPos)
- {
- this.url = url;
- this.localFile = savefile;
- this.startPos = startPos;
- this.endPos = endPos;
- }
- @Override
- public void run()
- {
- System.out.println(Thread.currentThread().getName() + "開始下載...");
- try
- {
- HttpURLConnection conn = (HttpURLConnection) url
- .openConnection();
- conn.setConnectTimeout(15 * 1000);
- conn.setRequestMethod("GET");
- conn.setRequestProperty(
- "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("Referer", url.toString());
- conn.setRequestProperty("Charset", "UTF-8");
- conn.setRequestProperty("Range", "bytes=" + startPos + "-"
- + endPos);// 設置獲取實體數據的範圍
- conn.setRequestProperty(
- "User-Agent",
- "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
- conn.setRequestProperty("Connection", "Keep-Alive");
- conn.connect();
- /**
- * 代表服務器已經成功處理了部分GET請求
- */
- if (conn.getResponseCode() == 206)
- {
- InputStream is = conn.getInputStream();
- int len = 0;
- byte[] buf = new byte[1024];
- RandomAccessFile raf = new RandomAccessFile(localFile,
- "rwd");
- raf.seek(startPos);
- while ((len = is.read(buf)) != -1)
- {
- raf.write(buf, 0, len);
- }
- raf.close();
- is.close();
- System.out.println(Thread.currentThread().getName()
- + "完成下載 : " + startPos + " -- " + endPos);
- this.isFinish = true;
- } else
- {
- throw new RuntimeException(
- "url that you conneted has error ...");
- }
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
多線程下載封裝類(轉http://blog.csdn.net/lmj623565791/article/details/26994463)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.