现在的位置: 首页 > 综合 > 正文

jsp 完成文件下载

2013年04月21日 ⁄ 综合 ⁄ 共 894字 ⁄ 字号 评论关闭

 <%
          
         
     String filename ="D:/t.txt";

     String filecopy = "t.txt";
     java.io.BufferedInputStream bis = null;
  java.io.BufferedOutputStream bos = null;
 
     try{
      File f = new File(filename);
      response.reset();
      OutputStream os = response.getOutputStream();    
  response.setContentType("application/x-msdownload");
        response.setContentType("application/vnd.wap.wmlc");
  response.setHeader("Content-disposition",
    "attachment; filename=/"" + filecopy + "/"");
  response.setContentLength((int) f.length());
    
  bis = new java.io.BufferedInputStream(
    new java.io.FileInputStream(filename));
    
  bos = new java.io.BufferedOutputStream(os);

  byte[] buff = new byte[1024];
  int bytesRead;
  
  while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
    bos.write(buff,0, bytesRead);
  }

 } catch (Exception e) {

  e.printStackTrace();
 } finally {
  if (bis != null)
   bis.close();
  if (bos != null)
   bos.close();
 }
  
   %>

抱歉!评论已关闭.