Servlet实现控制文件下载速度

jopen 11年前

  long speed =1024*100L;//限制下载速度为100k/s,    long current=0;    ServletOutputStream out = resp.getOutputStream();        File file =new File("xxxxxx");    FileInputStream in = new FileInputStream(file);    byte[]temp=new byte[1024];    int i;    long startTime=System.currentTimeMillis();    while((i=in.read(temp))!=-1){     current=current+i;     out.write(temp);     if(current>speed){      startPause(startTime+1000);      current=0;      startTime=System.currentTimeMillis();     }    }    in.close();   }      private void startPause(long time){    while(true){     if(System.currentTimeMillis()>time){      break;     }    }   }