文件下載

Code:
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>   
  2. <%@page import="java.io.OutputStream"%>   
  3. <%@page import="java.io.FileInputStream"%>   
  4. <%@page import="java.io.File"%>   
  5.  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  8. <html>   
  9.     <head>   
  10.         <base href="<%=basePath%>">   
  11.   
  12.         <title>下載</title>   
  13.   
  14.         <meta http-equiv="pragma" content="no-cache">   
  15.         <meta http-equiv="cache-control" content="no-cache">   
  16.         <meta http-equiv="expires" content="0">   
  17.           
  18.         
  19.         <!--   
  20.     <link rel="stylesheet" type="text/css" href="styles.css">   
  21.     -->   
  22.   
  23.     </head>   
  24.   
  25.     <body>   
  26.         <%   
  27.           String filename = request.getParameter("filename");   
  28.           String paths = request.getRealPath("/");   
  29.           response.setContentType("application/x-download");   
  30.           response.addHeader("Content-Disposition""attachment;filename="  
  31.                     + filename);   
  32.            
  33.           OutputStream op = null;   
  34.           FileInputStream in = null;   
  35.             try {   
  36.                 op = response.getOutputStream();   
  37.                 in = new FileInputStream(paths+"/upload/"+filename);   
  38.   
  39.                 byte[] b = new byte[1024];   
  40.                 int i = 0;   
  41.   
  42.                 while ((i = in.read(b)) > 0) {   
  43.                     op.write(b, 0, i);   
  44.                 }   
  45.                 op.flush();   
  46.             } catch (Exception e) {   
  47.                 e.printStackTrace();   
  48.             } finally {   
  49.                 if (in != null) {   
  50.                     in.close();   
  51.                     in = null;   
  52.                 }   
  53.                 if (op != null) {   
  54.                     op.close();   
  55.                     op = null;   
  56.                 }   
  57.             }   
  58.             File f = new File(application.getRealPath(filename));    
  59.             try {   
  60.                 f.delete();   
  61.                 f.deleteOnExit();   
  62.             } catch (Exception e) {   
  63.             }   
  64.             out.clear();   
  65.             out = pageContext.pushBody();   
  66.         %>   
  67.     </body>   
  68. </html>  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章