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

          雪山飛鵠

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

          BlogJava 首頁(yè) 新隨筆 聯(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">哈哈,測(cè)試文件下載</a>
          posted on 2010-12-10 17:21 雪山飛鵠 閱讀(21742) 評(píng)論(9)  編輯  收藏 所屬分類: javase

          Feedback

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

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

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

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

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

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

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

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

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

          主站蜘蛛池模板: 永州市| 夹江县| 阿城市| 凤凰县| 鸡东县| 伊川县| 麻栗坡县| 河源市| 凤城市| 遵化市| 固始县| 东至县| 龙川县| 巴中市| 建宁县| 拜城县| 濉溪县| 岗巴县| 喀什市| 陵水| 迁安市| 永靖县| 元氏县| 安龙县| 洛隆县| 吉隆县| 通许县| 万载县| 涞水县| 鲜城| 涪陵区| 揭西县| 枞阳县| 景泰县| 仁布县| 关岭| 分宜县| 延川县| 普洱| 阿克| 霍邱县|