溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫此文的意義!!!煩請您在轉(zhuǎn)載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫此文的意義!!!煩請您在轉(zhuǎn)載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
          package com.future.zfs.util;

          import java.io.BufferedInputStream;
          import java.io.BufferedOutputStream;
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.OutputStream;

          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          @SuppressWarnings(
          "serial")
          public class FileDownLoadServlet extends HttpServlet{
              
              @Override
              
          protected void service(HttpServletRequest request, HttpServletResponse response)
                      
          throws ServletException, IOException {
                  String path
          =request.getParameter("filename");
                  path
          =new String(path.getBytes("ISO-8859-1"),"utf-8");
                  download(path,request,response);
              }
              
              @SuppressWarnings(
          "deprecation")
              
          public HttpServletResponse download(String path,HttpServletRequest request, HttpServletResponse response) {
                  
          try {
                      
          // path是指欲下載的文件的路徑。
                      File file = new File(request.getRealPath("/")+"/"+path);
                      
          // 取得文件名。
                      String filename = file.getName();
                      
          // 取得文件的后綴名。
                      
          //String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

                      
          // 以流的形式下載文件。
                      InputStream fis = new BufferedInputStream(new FileInputStream(file));
                      
          byte[] buffer = new byte[fis.available()];
                      fis.read(buffer);
                      fis.close();
                      
          // 清空response
                      response.reset();
                      
          // 設(shè)置response的Header
                      response.addHeader("Content-Disposition""attachment;filename=" + new String(filename.getBytes("utf-8"),"ISO-8859-1"));
                      response.addHeader(
          "Content-Length""" + file.length());
                      OutputStream toClient 
          = new BufferedOutputStream(response.getOutputStream());
                      response.setContentType(
          "application/octet-stream");
                      toClient.write(buffer);
                      toClient.flush();
                      toClient.close();
                  } 
          catch (IOException ex) {
                      ex.printStackTrace();
                  }
                  
          return response;
              }
          }
          web.xml
          <servlet>
                  
          <servlet-name>fileDownLoadServlet</servlet-name>
                  
          <servlet-class>com.future.zfs.util.FileDownLoadServlet</servlet-class>
              
          </servlet>
              
          <servlet-mapping>
                  
          <servlet-name>fileDownLoadServlet</servlet-name>
                  
          <url-pattern>/fileDownLoadServlet</url-pattern>
              
          </servlet-mapping>
          <href="fileDownLoadServlet?filename=通訊錄.xls">哈哈,測試文件下載</a>
          posted on 2010-12-10 17:21 雪山飛鵠 閱讀(21748) 評論(9)  編輯  收藏 所屬分類: javase

          Feedback

          # re: 基于servlet的文件下載 2010-12-14 11:15 雪落無聲
          不錯,收藏了  回復(fù)  更多評論
            

          # re: 基于servlet的文件下載 2011-12-02 15:47 www
          @雪落無聲
          請問你的那個path是指文件在服務(wù)器的存儲路徑嗎
            回復(fù)  更多評論
            

          # re: 基于servlet的文件下載 2012-09-21 17:04 Laura
          不能指定 下載的路徑  回復(fù)  更多評論
            

          # re: 基于servlet的文件下載 2013-01-22 09:13 donny
          很不錯。根據(jù)你寫的我改了一改。就能指定下載路徑了。感謝  回復(fù)  更多評論
            

          # re: 基于servlet的文件下載 2013-05-20 16:13 gelingolf
          寫的不錯,學(xué)習(xí)
          www.gelingolf.com  回復(fù)  更多評論
            

          # re: 基于servlet的文件下載 2013-06-08 13:37 kzt
          文件大的話豈不是就不行了  回復(fù)  更多評論
            

          # re: 基于servlet的文件下載 2013-06-25 20:59 EasonHaha
          inputStream.avaliable()不安全吧,還是byte數(shù)組循環(huán)文檔些  回復(fù)  更多評論
            

          # re: 基于servlet的文件下載 2013-10-28 20:49 pecan
          沒錯@EasonHaha,這個方法已經(jīng)導(dǎo)致我的程序溢出了。。  回復(fù)  更多評論
            

          # re: 基于servlet的文件下載 2014-10-31 15:59 lyle
          深圳java群 397083120 找工作,討論技術(shù),交朋友  回復(fù)  更多評論
            

          主站蜘蛛池模板: 东宁县| 昌黎县| 图们市| 库尔勒市| 阿合奇县| 正宁县| 丁青县| 太原市| 前郭尔| 西乌| 沅江市| 工布江达县| 十堰市| 阿克苏市| 迁西县| 柳河县| 青阳县| 龙陵县| 广灵县| 西贡区| 昌江| 勐海县| 枝江市| 阳曲县| 凭祥市| 阳信县| 吐鲁番市| 武城县| 准格尔旗| 鲁甸县| 大悟县| 彝良县| 柏乡县| 台州市| 九龙县| 雷波县| 合水县| 奈曼旗| 中方县| 防城港市| 丰宁|