internet资源下载的断点续传

package com.hover.net;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class BreakPoints {

 private long start;
 private long end;
 
 public BreakPoints(long start,long end){
  this.start = start;
  this.end = end;
 }
 
 public BreakPoints(long start){
  this.start = start;
 }
 
 public void down(){
  URL url = null;
  String urlAddr = "http://zhangmenshiting.baidu.com/data2/music/116965837/14945107241200128.mp3?xcode=400d192b99ded71cebeb7b7364df3a0a4452c07fc0829579";
  URLConnection conn = null;
  InputStream is = null;
  FileOutputStream os = null;
  try {
   url = new URL(urlAddr);
   conn = url.openConnection();
   conn.setRequestProperty("User-Agent", "NetFox");
   String sProperty = "bytes="+start+"-";
   if(end > 0){
    sProperty = "bytes="+start+"-"+end;
   }
   conn.setRequestProperty("RANGE", sProperty);;
   conn.connect();
   is = conn.getInputStream();
   String name = url.getFile();
   String fileName = name.substring(name.lastIndexOf('/')+1);
   os = new FileOutputStream("F:"+File.separator+"as.mp3",true);
   byte[] buffer = new byte[1024];
   int index = -1;
   while((index=is.read(buffer))!=-1){
    os.write(buffer, 0, index);
   }
   os.close();
   is.close();
   conn.connect();
  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 public static void main(String[] args) {
  new BreakPoints(0).down();
 }
}

 

posted on 2014-04-09 16:30  持知以恒  阅读(165)  评论(0编辑  收藏  举报