neverend的日志

          不記錄,終將被遺忘。 一萬年太久,只爭(zhēng)朝夕。 他們用數(shù)字構(gòu)建了整個(gè)世界。

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            62 Posts :: 1 Stories :: 17 Comments :: 0 Trackbacks
          昨天做了一個(gè)Servlet,實(shí)現(xiàn)文檔下載功能。
          文檔路徑: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>請(qǐng)單擊下載按鈕下載文件</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的幾個(gè)方法的解讀。
          response.setHeader("Content-disposition", "attachment;filename=temp.doc");
          //設(shè)置響應(yīng)頭信息,讓下載的文件顯示保存信息

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

          response.setContentLength((int)file.length());
          //設(shè)置輸出文件長(zhǎng)度

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

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 拉萨市| 上林县| 镇雄县| 弋阳县| 韶山市| 依安县| 沙湾县| 徐汇区| 屏南县| 临桂县| 禄丰县| 雅安市| 蒲城县| 额敏县| 曲靖市| 定西市| 施秉县| 东乡县| 仙居县| 保定市| 彰化市| 固原市| 邵东县| 浦东新区| 定远县| 吉隆县| 达州市| 东乡| 普格县| 扎鲁特旗| 洛隆县| 来安县| 衡阳县| 顺义区| 汪清县| 南京市| 五常市| 静安区| 沁源县| 绍兴市| 北票市|