java尝试HTTP多线程断点续传下载

来源:互联网 发布:旋转矩阵 坐标空间 点 编辑:程序博客网 时间:2024/05/22 10:57

功能介绍:点击在新窗口中浏览此图片1 多线程HTTP下载2 支持断点续传3 临时文件下载,成功后改名4 提供防盗链的破解view plainprint? 1. package net.heck.tools; 2. 3. import java.io.BufferedInputStream; 4. import java.io.BufferedWriter; 5. import java.io.File; 6. import java.io.OutputStreamWriter; 7. import java.io.RandomAccessFile; 8. import java.net.Socket; 9. import java.net.URL; 10. import java.net.URLConnection; 11. import java.util.HashMap; 12. import java.util.Map; 13. 14. /** 15. * HTTP的多线程下载工具。 16. */ 17. public class HTTPDownloader extends Thread { 18. // 要下载的页面 19. private String page; 20. 21. // 保存的路径 22. private String savePath; 23. 24. // 线程数 25. private int threadNumber = 2; 26. 27. // 来源地址 28. private String referer; 29. 30. private String cookie; 31. 32. int threadPointer = 0; 33. 34. private Map threadPool = new HashMap(); // 线程迟 35. 36. // 最小的块尺寸。如果文件尺寸除以线程数小于这个,则会减少线程数。 37. private int MIN_BLOCK = 10 * 1024; 38. 39. public static void main(String[] args) throws Exception { 40. HTTPDownloader d = new HTTPDownloader("http://www.xxxxx.com/a.rar", null, "d://a.rar", 10, null); 41. d.down(); 42. } 43. 44. public void run() { 45. try { 46. down(); 47. } catch (Exception e) { 48. e.printStackTrace(); 49. } 50. } 51. 52. /** 53. * 下载操作 54. * 55. * @throws Exception 56. */ 57. public void down() throws Exception { 58. URL url = new URL(page); // 创建URL 59. URLConnection con = url.openConnection(); // 建立连接 60. con.setRequestProperty("Referer", referer == null ? page : referer); 61. con.setRequestProperty("UserAgent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; flashget)"); 62. int contentLen = con.getContentLength(); // 获得资源长度 63. if ((contentLen / MIN_BLOCK + 1) < threadNumber) { 64. threadNumber = contentLen / MIN_BLOCK + 1; // 调整下载线程数 65. } 66. if (threadNumber > 10) { 67. threadNumber = 10; 68. } 69. int begin = 0; 70. int step = contentLen / threadNumber + 1; 71. int end = 0; 72. HTTPDownloaderThread thread; 73. for (threadPointer = 0; threadPointer < threadNumber; threadPointer++) { 74. end += step; 75. if (end > contentLen) { 76. end = contentLen; 77. } 78. thread = new HTTPDownloaderThread(this, threadPointer, begin, end); 79. threadPool.put(threadPointer, thread); 80. thread.start(); 81. begin = end; 82. } 83. } 84. 85. /** 86. * 一个线程完活了。 87. * 88. * @param id 完活的线程id 89. */ 90. public synchronized void finished(int id) { 91. threadNumber--; 92. threadPool.remove(id); 93. if (threadNumber <= 0) { 94. System.out.println("FINISHED:" + savePath); 95. File f1 = new File(savePath + ".tmp"); 96. File f2 = new File(savePath); 97. // 如果目标文件已经存在,则尝试删除它 98. // 最多尝试3次,间隔1秒钟。 99. int times = 3; 100. while (f2.exists() && times > 0) { 101. if (f2.delete()) { 102. break; 103. } 104. try { 105. Thread.sleep(1000); 106. } catch (InterruptedException e) { 107. e.printStackTrace(); 108. } 109. times--; 110. } 111. if (!f2.exists()) { 112. if (!f1.renameTo(f2)) { 113. System.out.println("改名失败!"); 114. } 115. } else { 116. System.out.println("目标文件存在,且无法删除,无法改名"); 117. } 118. } else { 119. int size = 0; 120. HTTPDownloaderThread o = null; 121. // 尝试查找一个可以分担的线程 122. for (HTTPDownloaderThread thread : threadPool.values()) { 123. if (thread.endPos - thread.curPos > size) { 124. size = thread.endPos - thread.curPos; 125. o = thread; 126. } 127. } 128. if (size > MIN_BLOCK * 2) { 129. if (o.isAlive()) { 130. int endPos = o.endPos; 131. int beginPos = o.endPos - ((o.endPos - o.curPos) / 2); 132. o.endPos = beginPos; 133. threadNumber++; 134. threadPointer++; 135. HTTPDownloaderThread thread = new HTTPDownloaderThread(this, threadPointer, beginPos, endPos); 136. threadPool.put(threadPointer, thread); 137. System.out.println("A Help Thread for " + o.id + " is started with:" + threadPointer); 138. thread.start(); 139. } 140. } 141. } 142. } 143. 144. public HTTPDownloader() { 145. } 146. 147. /** 148. * 下载 149. * 150. * @param page 被下载的页面 151. * @param savePath 保存的路径 152. */ 153. public HTTPDownloader(String page, String savePath) { 154. this(page, savePath, 10); 155. } 156. 157. /** 158. * 下载 159. * 160. * @param page 被下载的页面 161. * @param savePath 保存的路径 162. * @param threadNumber 线程数 163. */ 164. public HTTPDownloader(String page, String savePath, int threadNumber) { 165. this(page, page, savePath, 10, null); 166. } 167. 168. /** 169. * 下载 170. * 171. * @param page 被下载的页面 172. * @param savePath 保存的路径 173. * @param threadNumber 线程数 174. * @param referer 来源 175. */ 176. public HTTPDownloader(String page, String referer, String savePath, int threadNumber, String cookie) { 177. this.page = page; 178. this.savePath = savePath; 179. this.threadNumber = threadNumber; 180. this.referer = referer; 181. } 182. 183. public String getPage() { 184. return page; 185. } 186. 187. public void setPage(String page) { 188. this.page = page; 189. } 190. 191. public String getSavePath() { 192. return savePath; 193. } 194. 195. public void setSavePath(String savePath) { 196. this.savePath = savePath; 197. } 198. 199. public int getThreadNumber() { 200. return threadNumber; 201. } 202. 203. public void setThreadNumber(int threadNumber) { 204. this.threadNumber = threadNumber; 205. } 206. 207. public String getReferer() { 208. return referer; 209. } 210. 211. public void setReferer(String referer) { 212. this.referer = referer; 213. } 214. 215. public String getCookie() { 216. return cookie; 217. } 218. 219. public void setCookie(String cookie) { 220. this.cookie = cookie; 221. } 222. } 223. 224. /** 225. * 下载线程 226. * 227. */ 228. class HTTPDownloaderThread extends Thread { 229. HTTPDownloader manager; 230. 231. int startPos; 232. 233. int endPos; 234. 235. int id; 236. 237. int curPos; 238. 239. int BUFFER_SIZE = 40960; 240. 241. int readByte = 0; 242. 243. HTTPDownloaderThread(HTTPDownloader manager, int id, int startPos, int endPos) { 244. this.id = id; 245. this.manager = manager; 246. this.startPos = startPos; 247. this.endPos = endPos; 248. } 249. 250. public void run() { 251. System.out.println("线程" + id + "启动," + startPos + "-" + endPos); 252. // 创建一个buff 253. BufferedInputStream bis = null; 254. RandomAccessFile fos = null; 255. // 缓冲区大小 256. byte[] buf = new byte[BUFFER_SIZE]; 257. boolean timeout = false; 258. Socket socket = null; 259. try { 260. curPos = startPos; 261. File file = new File(manager.getSavePath() + ".tmp"); 262. // 创建RandomAccessFile 263. fos = new RandomAccessFile(file, "rw"); 264. // 从startPos开始 265. fos.seek(startPos); 266. int index = manager.getPage().indexOf("/", 8); 267. String host = manager.getPage().substring(7, index); 268. // System.out.println(host); 269. socket = new Socket(host, 80); 270. socket.setSoTimeout(30000); 271. // 写入数据 272. BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8")); 273. StringBuilder b = new StringBuilder(); 274. b.append("GET " + manager.getPage().substring(index) + " HTTP/1.1/r/n"); 275. b.append("Host: " + host + "/r/n"); 276. b.append("Referer: " + (manager.getReferer() == null ? manager.getPage() : manager.getReferer()) + "/r/n"); 277. b.append("UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; flashget; /r/n"); 278. b.append("Range: bytes=" + startPos + "-" + endPos + "/r/n"); 279. b.append("/r/n"); 280. // System.out.println(b.toString()); 281. wr.write(b.toString()); 282. wr.flush(); 283. // 下面一段向根据文件写入数据,curPos为当前写入的未知,这里会判断是否小于endPos, 284. // 如果超过endPos就代表该线程已经执行完毕 285. bis = new BufferedInputStream(socket.getInputStream()); 286. // 读取直到换行 287. int ch; 288. boolean foundBR = false; 289. while (true) { 290. ch = bis.read(); 291. if (ch == 0xD) { 292. ch = bis.read(); 293. if (ch == 0xA) { 294. if (foundBR) { 295. break; 296. } 297. foundBR = true; 298. } else { 299. foundBR = false; 300. } 301. } else { 302. foundBR = false; 303. } 304. } 305. int len = -1; 306. while (curPos < endPos) { 307. // System.out.println(id + "=" + (endPos - curPos)); 308. len = bis.read(buf, 0, BUFFER_SIZE); 309. if (len == -1) { 310. break; 311. } 312. fos.write(buf, 0, len); 313. // System.out.println(id + "=Write OK!"); 314. curPos = curPos + len; 315. if (curPos > endPos) { 316. // 获取正确读取的字节数 317. readByte += len - (curPos - endPos) + 1; 318. } else { 319. readByte += len; 320. } 321. } 322. System.out.println("线程" + id + "已经下载完毕:" + readByte); 323. } catch (Exception ex) { 324. timeout = true; 325. } finally { 326. if (bis != null) { 327. try { 328. bis.close(); 329. } catch (Exception e) { 330. System.out.println("关闭文件失败(1)!"); 331. } 332. } 333. if (fos != null) { 334. try { 335. fos.close(); 336. } catch (Exception e) { 337. System.out.println("关闭文件失败(2)!"); 338. } 339. } 340. if (socket != null) { 341. try { 342. socket.close(); 343. } catch (Exception e) { 344. System.out.println("关闭链接失败!"); 345. } 346. } 347. } 348. if (timeout) { 349. System.out.println(id + " timeout, restart..."); 350. new HTTPDownloaderThread(manager, id, curPos, endPos).start(); 351. } else { 352. manager.finished(id); 353. } 354. } 355. }

原创粉丝点击