葉落知秋

          一葉落而知天下秋

          統(tǒng)計(jì)

          留言簿(1)

          Java相關(guān)

          閱讀排行榜

          評論排行榜

          下載服務(wù)的Servlet


           發(fā)表時間: 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 飛雪連天 閱讀(143) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 喜德县| 吴川市| 榕江县| 东城区| 吉林省| 华宁县| 濉溪县| 富民县| 惠来县| 银川市| 建宁县| 佛山市| 中江县| 广南县| 横山县| 军事| 尖扎县| 军事| 台中县| 昌黎县| 象山县| 宜春市| 永年县| 大宁县| 涞水县| 尚义县| 专栏| 德阳市| 建宁县| 拜城县| 华蓥市| 扎鲁特旗| 桃园市| 临邑县| 南投市| 广德县| 盐边县| 都兰县| 清原| 怀来县| 山阴县|