Java下載檔案(以TIM為例)
阿新 • • 發佈:2018-12-21
package download; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; public class Downloader { public static void main(String[] args) throws Exception { String filename = "tim_pc.exe"; String path = "https://qd.myapp.com/myapp/qqteam/tim/down/tim_pc.exe"; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); // 獲得需要下載的檔案的長度(大小) int filelength = conn.getContentLength(); System.out.println("要下載的檔案長度" + filelength); // 生成一個大小相同的本地檔案 RandomAccessFile file = new RandomAccessFile(filename, "rwd"); file.setLength(filelength); file.close(); conn.disconnect(); // 設定有多少條執行緒下載 int threadsize = 1; // 計算每個執行緒下載的量 int threadlength = filelength % 3 == 0 ? filelength / 3 : filelength + 1; for (int i = 0; i < threadsize; i++) { // 設定每條執行緒從哪個位置開始下載 int startposition = i * threadlength; // 從檔案的什麼位置開始寫入資料 RandomAccessFile threadfile = new RandomAccessFile(filename, "rwd"); threadfile.seek(startposition); // 啟動三條執行緒分別從startposition位置開始下載檔案 new DownLoadThread(i, startposition, threadfile, threadlength, path) .start(); } int quit = System.in.read(); while ('q' != quit) { Thread.sleep(2000); } } } // @Test // public void download()throws Exception{ // String fileName="QQSetUp.exe"; // // String path="https://qd.myapp.com/myapp/qqteam/tim/down/tim_pc.exe"; // // URL url=new URL(path); // // HttpURLConnection conn=(HttpURLConnection)url.openConnection(); // // conn.setConnectTimeout(5000); // conn.setRequestMethod("GET"); // // //獲得需要下載的檔案長度(大小) // int filelength=conn.getContentLength(); // System.out.print("檔案大小:"+filelength); // //生成一個大小相同的本地檔案 // RandomAccessFile file=new RandomAccessFile(fileName,"rwd"); // file.setLength(filelength); // file.close(); // conn.disconnect(); // // //設定有多少個執行緒下載 // int threadsize=3; // int threadlength=filelength%3==0?filelength/3:filelength+1; // // for(int i=0;i<threadsize;i++){ // int startposition=i*threadlength; // RandomAccessFile threadfile =new RandomAccessFile(fileName,"rwd"); // threadfile.seek(startposition); // // //啟動三條執行緒分別從startposition位置開始下載檔案 // new DownLoadThread(i,startposition,threadfile,threadlength,path).start(); // } // // int quit=System.in.read(); // while('q'!=quit){ // Thread.sleep(2000); // } // } // }
package download; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; public class DownLoadThread extends Thread { private int threadid; private int startposition; private RandomAccessFile threadfile; private int threadlength; private String path; public DownLoadThread(int threadid, int startposition, RandomAccessFile threadfile, int threadlength, String path) { this.threadid = threadid; this.startposition = startposition; this.threadfile = threadfile; this.threadlength = threadlength; this.path = path; } public DownLoadThread() { } @Override public void run() { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); // 指定從什麼位置開始下載 conn.setRequestProperty("Range", "bytes=" + startposition + "-"); // System.out.println(conn.getResponseCode()); if (conn.getResponseCode() == 206) { InputStream is = conn.getInputStream(); byte[] buffer = new byte[1024]; int len = -1; int length = 0; while (length < threadlength && (len = is.read(buffer)) != -1) { threadfile.write(buffer, 0, len); // 計算累計下載的長度 length += len; } threadfile.close(); is.close(); System.out.println("執行緒" + (threadid + 1) + "已下載完成"); } } catch (Exception ex) { System.out.println("執行緒" + (threadid + 1) + "下載出錯" + ex); } } }