JAVA 控制客户端下载是否完成

<%@ page contentType="text/html;charset=gb2312" import="java.io.*"%>
<%
  try{
 
  String str=request.getParameter("name1");//获得响应客户的输出流
 
  str=new String(str.getBytes("iso8859-1"),"gb2312");
 
  File fileLoad=new File("f:\\tools\\xiazai",str);//下载文件
 
  response.reset();  
 
  OutputStream o=response.getOutputStream();
 
  BufferedOutputStream bos=new BufferedOutputStream(o);
 
  byte b[]=new byte[500];//输出文件用的字节数组,每次发送500个字节到输出流


  response.setHeader("Content-disposition","attachment;filename="+new String(str.getBytes("gb2312"),"iso8859-1"));


  response.setContentType("application/x-tar");//通知客户文件的mime类型


  long fileLength=fileLoad.length();//通知客户文件的长度


  String length=String.valueOf(fileLength);
 
  response.setHeader("Content_Length",length);  
 
  FileInputStream in=new FileInputStream(fileLoad);//读取文件并发送给客户下载


  int n=0;


  while((n=in.read(b))!=-1)
  {
      bos.write(b,0,n);
  }


  bos.close();


  }
    catch(Exception e){


out.println("发生了异常"+e);


}


response.reset();


%>


就是那些红色的部分,希望大家讲解下,谢谢!

posted on 2009-11-03 11:30  草原和大树  阅读(272)  评论(0编辑  收藏  举报