叶落知秋

一叶落而知天下秋

统计

留言簿(1)

Java相关

阅读排行榜

评论排行榜

下载服务的Servlet


 发表时间: 2007年11月07日 



java 代码
  1. import java.io.File;   
  2. import java.io.FileInputStream;   
  3. import java.io.IOException;   
  4. import java.io.OutputStream;   
  5.   
  6. import javax.servlet.ServletException;   
  7. import javax.servlet.http.HttpServlet;   
  8. import javax.servlet.http.HttpServletRequest;   
  9. import javax.servlet.http.HttpServletResponse;   
  10.   
  11. import org.apache.log4j.Logger;   
  12.   
  13.   
  14.   
  15. public class DownloadRscFileServlet extends HttpServlet {   
  16.     private Logger log = Logger.getLogger(this.getClass());   
  17.        
  18.     /**  
  19.      * Constructor of the object.  
  20.      */  
  21.     public DownloadRscFileServlet() {   
  22.         super();   
  23.     }   
  24.   
  25.     /**  
  26.      * Destruction of the servlet. <br>  
  27.      */  
  28.     public void destroy() {   
  29.         super.destroy(); // Just puts "destroy" string in log   
  30.         // Put your code here   
  31.     }   
  32.   
  33.     /**  
  34.      * The doGet method of the servlet. <br>  
  35.      *   
  36.      * This method is called when a form has its tag value method equals to get.  
  37.      *   
  38.      * @param request the request send by the client to the server  
  39.      * @param response the response send by the server to the client  
  40.      * @throws ServletException if an error occurred  
  41.      * @throws IOException if an error occurred  
  42.      */  
  43.     public void doGet(HttpServletRequest request, HttpServletResponse response)   
  44.             throws ServletException, IOException {   
  45.         String rscFileName = request.getParameter("rscFileName");   
  46.     
  47.         if(rscFileName == null || rscFileName.equals("")){   
  48.             log.debug(   
  49.                 "Invaild request:can not get type from request!");   
  50.             return;   
  51.         }   
  52.         String path = "d:/upload";   
  53.         //String rscFileName = SMPConfig.getValue("");   
  54.         File rscFile = new File(path+"/"  + rscFileName);   
  55.         if(!rscFile.exists()){   
  56.             log.debug(   
  57.                 "In DownloadRscFileServlet..... RscFile does not exist! RscFileName:" +   
  58.                 rscFileName + " FileName:" + rscFile.getAbsolutePath());   
  59.             response.getWriter().println(rscFileName + " does not exist!");   
  60.             return;   
  61.         }   
  62.         response.setHeader("Content-disposition","attachment; filename=" + rscFile.getName());   
  63.         response.setContentType("application/x-msdownload");   
  64.         OutputStream out = response.getOutputStream();   
  65.         FileInputStream in = new FileInputStream(rscFile);   
  66.         int i = -1;   
  67.         while((i = in.read()) != -1){   
  68.             out.write(i);   
  69.         }   
  70.         in.close();   
  71.         out.close();           
  72.            
  73.     }   
  74.        
  75.     /**  
  76.      * The doGet method of the servlet. <br>  
  77.      *   
  78.      * This method is called when a form has its tag value method equals to get.  
  79.      *   
  80.      * @param request the request send by the client to the server  
  81.      * @param response the response send by the server to the client  
  82.      * @throws ServletException if an error occurred  
  83.      * @throws IOException if an error occurred  
  84.      */  
  85.     public void doPost(HttpServletRequest request, HttpServletResponse response)   
  86.             throws ServletException, IOException {   
  87.         doGet(request,response);   
  88.     }       
  89. }  

posted on 2007-11-07 17:22 飞雪连天 阅读(135) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: