HTTP多线程断点续传下载的尝试 - 编程入门网
作者 佚名技术
来源 NET编程
浏览
发布时间 2012-06-22
. 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. * @author 赵学庆 www.java2000.net 228. */ 229.class HTTPDownloaderThread extends Thread { 230. HTTPDownloader manager; 231. 232. int startPos; 233. 234. int endPos; 235. 236. int id; 237. 238. int curPos; 239. 240. int BUFFER_SIZE = 40960; 241. 242. int readByte = 0; 243. 244. HTTPDownloaderThread(HTTPDownloader manager, int id, int startPos, int endPos) { 245. this.id = id; 246. this.manager = manager; 247. this.startPos = startPos; 248. this.endPos = endPos; 249. } 250. 251. public void run() { 252. System.out.println("线程" + id + "启动," + startPos + "-" + endPos); 253. // 创建一个buff 254. BufferedInputStream bis = null; 255. RandomAccessFile fos = null; 256. // 缓冲区大小 257. byte[] buf = new byte[BUFFER_SIZE]; 258. boolean timeout = false; 259. Socket socket = null; 260. try { 261. curPos = startPos; 262. File file = new File(manager.getSavePath() + ".tmp"); 263. // 创建RandomAccessFile 264. fos = new RandomAccessFile(file, "rw"); 265. // 从startPos开始 266. fos.seek(startPos); 267. int index = manager.getPage().indexOf("/", 8); 268. String host = manager.getPage().substring(7, index); 269. // System.out.println(host); 270. socket = new Socket(host, 80); 271. socket.setSoTimeout(30000); 272. // 写入数据 273. BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8")); 274. StringBuilder b = new StringBuilder(); 275. b.append("GET " + manager.getPage().substring(index) + " HTTP/1.1\r\n"); 276. b.append("Host: " + host + "\r\n"); 277. b.append("Referer: " + (manager.getReferer() == null ? manager.getPage() : manager.getReferer()) + "\r\n"); 278. b.append("UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; flashget; \r\n"); 279. b.append("Range: bytes=" + startPos + "-" + endPos + "\r\n"); 280. b.append("\r\n"); 281. // System.out.println(b.toString()); 282. wr.write(b.toString()); 283. wr.flush(); 284. // 下面一段向根据文件写入数据,curPos为当前写入的未知,这里会判断是否小于endPos, 285. // 如果超过endPos就代表该线程已经执行完毕 286. bis = new BufferedInputSt |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于HTTP多线程断点续传下载的尝试 - 编程入门网的所有评论