neverend的日志

          不記錄,終將被遺忘。 一萬年太久,只爭朝夕。 他們用數字構建了整個世界。

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            62 Posts :: 1 Stories :: 17 Comments :: 0 Trackbacks
          昨天做了一個Servlet,實現文檔下載功能。
          文檔路徑:C:\test\temp.doc
          package Servlet;

          import java.io.*;
          import javax.servlet.*;
          import javax.servlet.http.*;

          public class Download extends HttpServlet{
           
          private static int DEFAULT_BUFFER_SIZE=1024*4;
           
          public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
            response.setContentType(
          "text/html");
            response.setCharacterEncoding(
          "GB2312");
            PrintWriter out
          =response.getWriter();
            out.println(
          "<HTML>");
            out.println(
          "<HEAD>");
            out.println(
          "<TITLE>");
            out.println(
          "下載");
            out.println(
          "</TITLE>");
            out.println(
          "</HEAD>");
            out.println(
          "<BODY>");
            out.println(
          "<form method=\"post\" action=\"download?file=c:\\test\\temp.doc\">");
            out.println(
          "<h2>請單擊下載按鈕下載文件</h2>");
            out.println(
          "<input type=\"submit\" value=\"下載\">");
            out.println(
          "</form>");
            out.println(
          "</BODY>");
            out.println(
          "</HTML>");
            out.close();
           }
           
          public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
            String fileName
          =(String)request.getParameter("file");
            ServletOutputStream output
          =null;
            FileInputStream input
          =null;
            File file
          =new File(fileName);
            
          if(!file.exists())
             
          throw new IOException("文件不存在");
           
          try{
            response.setHeader(
          "Content-disposition""attachment;filename=temp.doc");
            response.setContentType(
          "application/msword");
            response.setContentLength((
          int)file.length());
            output
          =response.getOutputStream();
            input
          =new FileInputStream(file);
            copy(input,output);
           }
          catch(IOException e){e.printStackTrace();}
            
          finally{if (output!=null)
             output.close();
            
          if(input!=null)
             input.close();
            }
            
           }
           
          private static int copy(InputStream input,OutputStream output)throws IOException{
            
          byte[] buffer=new byte[DEFAULT_BUFFER_SIZE];
            
          int count=0;
            
          int n=0;
            
          while(-1!=(n=input.read(buffer))){
             output.write(buffer, 
          0, n);
             count
          +=n;
            }
           
          return count;
           }
          }

          Servlet配置信息如下:
          <servlet>    
            <servlet-name>Download</servlet-name>    
            <display-name>download</display-name>    
            <description>A   Download Servlet</description>    
            <servlet-class>Servlet.Download</servlet-class>    
           </servlet>    
           <servlet-mapping>    
            <servlet-name>Download</servlet-name>    
            <url-pattern>/download</url-pattern>    
           </servlet-mapping>

          需要注意的問題。
          1.配置路徑為/download,不能是/Download.
          2.response的幾個方法的解讀。
          response.setHeader("Content-disposition", "attachment;filename=temp.doc");
          //設置響應頭信息,讓下載的文件顯示保存信息

          response.setContentType("application/msword");
          //設置下載文檔的類型,
          "application/msword"就是指word文檔。還有
          "application/pdf " :pdf文檔
          "application/zip":   zip文檔
          …………

          response.setContentLength((int)file.length());
          //設置輸出文件長度

          response.getOutputStream();
          //取得輸出流,用于向客戶發送二進制數據
          posted on 2008-08-18 21:52 neverend 閱讀(1819) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 沈丘县| 神农架林区| 咸丰县| 固原市| 临夏县| 雷州市| 庆城县| 富裕县| 诸城市| 丹东市| 洪江市| 岗巴县| 呼图壁县| 阜宁县| 襄汾县| 万载县| 钟祥市| 珲春市| 呼和浩特市| 长海县| 乌拉特中旗| 宁城县| 房产| 赤壁市| 饶河县| 聂拉木县| 福泉市| 永吉县| 荆州市| 集安市| 十堰市| 申扎县| 阿城市| 台山市| 正镶白旗| 三门峡市| 井研县| 河池市| 革吉县| 金堂县| 噶尔县|