分享

如何用java获取网络文件的大小(多线程)

 pablo3518 2007-06-11
下载方法:
 
public void saveToFile(String destUrl, String fileName) throws IOException
{
    FileOutputStream fos = null;
    BufferedInputStream bis = null;
    HttpURLConnection httpUrl = null;
    URL url = null;
    byte[] buf = new byte[BUFFER_SIZE];
    int size = 0;
    int s=0;
    //建立链接
   url = new URL(destUrl);
    httpUrl = (HttpURLConnection) url.openConnection();
    //连接指定的资源
    httpUrl.connect();
    //获取网络输入流
    bis = new BufferedInputStream(httpUrl.getInputStream());
    //建立文件
    fos = new FileOutputStream(fileName);
   
System.out.println("正在获取链接[" + destUrl + "]的内容...\n将其保存为文件[" + fileName + "]");
    //保存文件
    while ( (size = bis.read(buf)) != -1)
    {
    fos.write(buf, 0, size);
    //++s;
    System.out.println(size);
    //System.out.println(s/1024+"M");
    }
     
    fos.close();
    bis.close();
    httpUrl.disconnect();
  }
 
多线程文件下载程序:
public class DownloadNetTest {
   private File fileOut;
   private URL url;
   private long fileLength=0;
   //初始化线程数
   private int ThreadNum=5;
   public DownloadNetTest(){
   try{
      System.out.println("正在链接URL");
      url=new URL("http://211.64.201.201/uploadfile/nyz.mp3");
      HttpURLConnection urlcon=(HttpURLConnection)url.openConnection();
      //根据响应获取文件大小
      fileLength=urlcon.getContentLength();
      if(urlcon.getResponseCode()>=400){
      System.out.println("服务器响应错误");
      System.exit(-1);
      }
      if(fileLength<=0)
      System.out.println("无法获知文件大小");
      //打印信息
      printMIME(urlcon);
      System.out.println("文件大小为"+fileLength/1024+"K");
      //获取文件名
      String trueurl=urlcon.getURL().toString();
      String filename=trueurl.substring(trueurl.lastIndexOf(‘/‘)+1);
      fileOut=new File("D://",filename);
   }
   catch(MalformedURLException e){
      System.err.println(e);
   }
   catch(IOException e){
      System.err.println(e);
   }
   init();
}
   private void init(){
      DownloadNetThread [] down=new DownloadNetThread[ThreadNum];
   try {
      for(int i=0;i<ThreadNum;i++){
         RandomAccessFile randOut=new RandomAccessFile(fileOut,"rw");
         randOut.setLength(fileLength);
         long block=fileLength/ThreadNum+1;
         randOut.seek(block*i);
         down[i]=new DownloadNetThread(url,randOut,block,i+1);
         down[i].setPriority(7);
         down[i].start();
      }
   //循环判断是否下载完毕
   boolean flag=true;
   while (flag) {
      Thread.sleep(500);
      flag = false;
      for (int i = 0; i < ThreadNum; i++)
      if (!down[i].isFinished()) {
      flag = true;
      break;
      }
  }// end while
   System.out.println("文件下载完毕,保存在"+fileOut.getPath() );
   } catch (FileNotFoundException e) {
         System.err.println(e);
         e.printStackTrace();
   }
   catch(IOException e){
      System.err.println(e);
      e.printStackTrace();
   }
   catch (InterruptedException e) {
   System.err.println(e);
   }
}
private void printMIME(HttpURLConnection http){
   for(int i=0;;i++){
   String mine=http.getHeaderField(i);
   if(mine==null)
   return;
   System.out.println(http.getHeaderFieldKey(i)+":"+mine);
   }
}
public static void main(String[] args) {
       DownloadNetTest app=new DownloadNetTest();
}
}

//线程类
public class DownloadNetThread extends Thread{
private InputStream randIn;
private RandomAccessFile randOut;
private URL url;
private long block;
private int threadId=-1;
private boolean done=false;
   public DownloadNetThread(URL url,RandomAccessFile out,long block,int threadId){
   this.url=url;
   this.randOut=out;
   this.block=block;
   this.threadId=threadId;
}
 public void run(){
   try{
      HttpURLConnection http=(HttpURLConnection)url.openConnection();
      http.setRequestProperty("Range","bytes="+block*(threadId-1)+"-");
      randIn=http.getInputStream();
   }
   catch(IOException e){
      System.err.println(e);
   }
   ////////////////////////
   byte [] buffer=new byte[1024];
   int offset=0;
   long localSize=0;
   System.out.println("线程"+threadId+"开始下载");
   try {
      while ((offset = randIn.read(buffer)) != -1&&localSize<=block) {
      randOut.write(buffer,0,offset);
      localSize+=offset;
      }
      randOut.close();
      randIn.close();
      done=true;
      System.out.println("线程"+threadId+"完成下载");
      this.interrupt();
   }
   catch(Exception e){
      System.err.println(e);
   }
}
  public boolean isFinished(){
     return done;
  }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多