Dict.CN 在線詞典, 英語學習, 在線翻譯

          都市淘沙者

          荔枝FM Everyone can be host

          統計

          留言簿(23)

          積分與排名

          優秀學習網站

          友情連接

          閱讀排行榜

          評論排行榜

          一個文件下載的Servlet

          把文件目錄直接暴露給用戶是很不安全的。所以要用Servlet來做,而且這樣做,文件的存儲方式就更豐富了,可以是從文件系統上取來的,也可以是數據庫中經過計算生成的,或者從其它什么稀奇古怪的地方取來的。

          public class DownloadServlet extends HttpServlet {
              
          private String contentType = "application/x-msdownload";
              
          private String enc = "utf-8";
              
          private String fileRoot = "";


              
          /**
               * 初始化contentType,enc,fileRoot
               
          */
              
          public void init(ServletConfig config) throws ServletException {
                  String tempStr 
          = config.getInitParameter("contentType");
                  
          if (tempStr != null && !tempStr.equals("")) {
                      contentType 
          = tempStr;
                  }
                  tempStr 
          = config.getInitParameter("enc");
                  
          if (tempStr != null && !tempStr.equals("")) {
                      enc 
          = tempStr;
                  }
                  tempStr 
          = config.getInitParameter("fileRoot");
                  
          if (tempStr != null && !tempStr.equals("")) {
                      fileRoot 
          = tempStr;
                  }
              }

              
          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                  String filepath 
          = request.getParameter("filepath");
                  String fullFilePath 
          = fileRoot + filepath;
                  
          /*讀取文件*/
                  File file 
          = new File(fullFilePath);
                  
          /*如果文件存在*/
                  
          if (file.exists()) {
                      String filename 
          = URLEncoder.encode(file.getName(), enc);
                      response.reset();
                      response.setContentType(contentType);
                      response.addHeader(
          "Content-Disposition""attachment; filename=\"" + filename + "\"");
                      
          int fileLength = (int) file.length();
                      response.setContentLength(fileLength);
                      
          /*如果文件長度大于0*/
                      
          if (fileLength != 0) {
                          
          /*創建輸入流*/
                          InputStream inStream 
          = new FileInputStream(file);
                          
          byte[] buf = new byte[4096];
                          
          /*創建輸出流*/
                          ServletOutputStream servletOS 
          = response.getOutputStream();
                          
          int readLength;
                          
          while (((readLength = inStream.read(buf)) != -1)) {
                              servletOS.write(buf, 
          0, readLength);
                          }
                          inStream.close();
                          servletOS.flush();
                          servletOS.close();
                      }
                  }
              }

          web.xml
              <servlet>
                  
          <servlet-name>downloadservlet-name>
                  
          <servlet-class>org.mstar.servlet.DownloadServletservlet-class>
                  
          <init-param>
                      
          <param-name>fileRootparam-name>
                      
          <param-value>d:/tempparam-value>
                  
          init-param>
                  
          <init-param>
                      
          <param-name>contentTypeparam-name>
                      
          <param-value>application/x-msdownloadparam-value>
                  
          init-param>
                  
          <init-param>
                      
          <param-name>encparam-name>
                      
          <param-value>utf-8param-value>
                  
          init-param>
              
          servlet>
              
          <servlet-mapping>
                  
          <servlet-name>downloadservlet-name>
                  
          <url-pattern>/downurl-pattern>
              
          servlet-mapping>

          posted on 2007-11-07 14:33 都市淘沙者 閱讀(434) 評論(0)  編輯  收藏 所屬分類: Java Basic/Lucene/開源資料

          主站蜘蛛池模板: 招远市| 若尔盖县| 白朗县| 清徐县| 洪洞县| 昭平县| 桦川县| 利津县| 北流市| 九寨沟县| 江华| 灵寿县| 云龙县| 江口县| 修武县| 来凤县| 利川市| 定远县| 东城区| 博乐市| 九龙县| 五华县| 常山县| 延长县| 海伦市| 象州县| 长子县| 丹棱县| 介休市| 贵港市| 卓尼县| 漠河县| 虞城县| 尼木县| 搜索| 洪泽县| 儋州市| 娱乐| 高阳县| 陵川县| 武强县|