1 package unit; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.File; 5 import java.io.FileOutputStream; 6 import java.io.InputStream; 7 import java.math.BigDecimal; 8 import java.net.URL; 9 import java.net.URLConnection; 10 import java.util.ArrayList; 11 import java.util.Collections; 12 import java.util.concurrent.ExecutorService; 13 import java.util.concurrent.Executors; 14 15 /** 16 * 文件下载 17 */ 18 public class HttpUtils { 19 static long sumContent = 0; 20 static float useTime = 0; 21 ArrayList<Float> speed = new ArrayList<Float>(); 22 public static void main(String[] args) { 23 String url ="http://xcy1.xiaoshikd.com/python3.zip\r\n"; 24 String dirPath = "D:/111/downLoad/"; 25 String dirPath2 = "D:/222/downLoad/"; 26 String dirPath3 = "D:/333/downLoad/"; 27 HttpUtils.download(url, dirPath, "============"); 28 HttpUtils.download(url, dirPath2, "============================"); 29 HttpUtils.download(url, dirPath3, "=============================================="); 30 } 31 32 public static void download(String url, String filePath, final String message) { 33 HttpUtils.getInstance().download(url, filePath, new HttpClientDownLoadProgress() { 34 @Override 35 public void onProgress(int progress) { 36 System.out.println("download progress "+message+ progress+"%"); 37 } 38 }); 39 } 40 41 /** 42 * 最大线程池 43 */ 44 public static final int THREAD_POOL_SIZE = 4; 45 46 public interface HttpClientDownLoadProgress { 47 public void onProgress(int progress); 48 } 49 50 private static HttpUtils httpClientDownload; 51 52 private ExecutorService downloadExcutorService; 53 54 private HttpUtils() { 55 downloadExcutorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE); 56 } 57 58 public static HttpUtils getInstance() { 59 if (httpClientDownload == null) { 60 httpClientDownload = new HttpUtils(); 61 } 62 return httpClientDownload; 63 } 64 65 /** 66 * 下载文件 67 * 68 * @param url 69 * @param filePath 70 * @param progress 71 * 进度回调 72 */ 73 public void download(final String url, final String filePath, final HttpClientDownLoadProgress progress) { 74 downloadExcutorService.execute(new Runnable() { 75 @Override 76 public void run() { 77 httpDownloadFile(url, filePath, progress); 78 } 79 }); 80 } 81 82 /** 83 * 下载文件 84 * @param url 85 * @param filePath 86 */ 87 private void httpDownloadFile(String strUrl, String filePath, HttpClientDownLoadProgress progress) { 88 try { 89 long startTime = System.currentTimeMillis(); 90 URL url = new URL(strUrl); 91 String file = url.getFile(); 92 String fileName = file.substring(file.lastIndexOf('/')+1); 93 URLConnection conn = url.openConnection(); 94 InputStream is = conn.getInputStream(); 95 long contentLength = conn.getContentLength(); 96 ByteArrayOutputStream output = new ByteArrayOutputStream(); 97 byte[] buffer = new byte[65536]; 98 int r = 0; 99 long totalRead = 0; 100 while ((r = is.read(buffer)) > 0) { 101 output.write(buffer, 0, r); 102 totalRead += r; 103 sumContent+=r; 104 if (progress != null) {// 回调进度 105 progress.onProgress((int) (totalRead * 100 / contentLength)); 106 } 107 } 108 109 /** 110 * 将下载文件写入本地 111 */ 112 File f = new File(filePath); 113 if(!f.exists()) { 114 f.mkdirs(); 115 } 116 filePath = filePath+fileName; 117 FileOutputStream fos = new FileOutputStream(filePath); 118 output.writeTo(fos); 119 output.flush(); 120 121 Long endTime = System.currentTimeMillis(); 122 useTime = (float)(endTime-startTime)/1000; 123 getDoloadResult(sumContent, useTime); 124 125 output.close(); 126 fos.close(); 127 is.close(); 128 downloadExcutorService.shutdown(); 129 } catch (Exception e) { 130 e.printStackTrace(); 131 downloadExcutorService.shutdown(); 132 } 133 } 134 135 public void getDoloadResult(long contentLength, float useTime) { 136 System.out.println("sumContentLength: "+contentLength); 137 System.out.println("useTime: "+useTime); 138 139 float bySpead = contentLength/useTime/1024/1024; 140 BigDecimal b = new BigDecimal(bySpead); 141 bySpead = b.setScale(2, 4).floatValue();; 142 speed.add(bySpead); 143 System.out.println("avgSpeed: "+bySpead+" M/s"); 144 System.out.println("maxSpeed: "+Collections.max(speed)+" M/s"); 145 } 146 }