用Java实现HTTP断点续传 - 编程入门网
作者 佚名技术
来源 NET编程
浏览
发布时间 2012-06-22
+ nPos[nPos.length-1] + ", nEndPos = " + nFileLength); // fileSplitterFetch[nPos.length-1].start(); //等待子线程结束 //int count = 0; //是否结束while循环 boolean breakWhile = false; while(!bStop) { write_nPos(); Utility.sleep(500); breakWhile = true; for(int i=0;i<nStartPos.length;i++) { if(!fileSplitterFetch[i].bDownOver) { breakWhile = false; break; } } if(breakWhile) break; //count++; //if(count>4) // siteStop(); } System.err.println("文件下载结束!"); } catch(Exception e){e.printStackTrace ();} } //获得文件长度 public long getFileSize() { int nFileLength = -1; try{ URL url = new URL(siteInfoBean.getSSiteURL()); HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection (); httpConnection.setRequestProperty("User-Agent","NetFox"); int responseCode=httpConnection.getResponseCode(); if(responseCode>=400) { processErrorCode(responseCode); return -2; //-2 represent access is error } String sHeader; for(int i=1;;i++) { //DataInputStream in = new DataInputStream(httpConnection.getInputStream ()); //Utility.log(in.readLine()); sHeader=httpConnection.getHeaderFieldKey(i); if(sHeader!=null) { if(sHeader.equals("Content-Length")) { nFileLength = Integer.parseInt(httpConnection.getHeaderField(sHeader)); break; } } else break; } } catch(IOException e){e.printStackTrace ();} catch(Exception e){e.printStackTrace ();} Utility.log(nFileLength); return nFileLength; } //保存下载信息(文件指针位置) private void write_nPos() { try{ output = new DataOutputStream(new FileOutputStream(tmpFile)); output.writeInt(nStartPos.length); for(int i=0;i<nStartPos.length;i++) { // output.writeLong(nPos[i]); output.writeLong(fileSplitterFetch[i].nStartPos); output.writeLong(fileSplitterFetch[i].nEndPos); } output.close(); } catch(IOException e){e.printStackTrace ();} catch(Exception e){e.printStackTrace ();} } //读取保存的下载信息(文件指针位置) private void read_nPos() { try{ DataInputStream input = new DataInputStream(new FileInputStream(tmpFile)); int nCount = input.readInt(); nStartPos = new long[nCount]; nEndPos = new long[nCount]; for(int i=0;i<nStartPos.length;i++) { nStartPos[i] = input.readLong(); nEndPos[i] = input.readLong(); } input.close(); } catch(IOException e){e.printStackTrace ();} catch(Exception e){e.printStackTrace ();} } private void processErrorCode(int nErrorCode) { System.err.println("Error Code : " + nErrorCode); } //停止文件下载 public void siteStop() { bStop = true; for(int i=0;i<nStartPos.length;i++) fileS |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于用Java实现HTTP断点续传 - 编程入门网的所有评论