五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > Servlet實(shí)現(xiàn)文件下載

Servlet實(shí)現(xiàn)文件下載

文章來源:365jz.com     點(diǎn)擊數(shù):873    更新時(shí)間:2009-10-12 09:42   參與評論

1.JSP頁:

<%--
    Document   : DownloadFile
    Created on : 2009-10-11, 21:24:22
    Author     : lucifer
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
         <form method="post" action="DownLoadFileServlet">
              密碼:<input type="text" name="password"><br>
              <input type="submit" value="submit">
         </form>
    </body>
</html>


2.Servlet(DownLoadFileServlet):

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
 *
 * @author lucifer
 */
public class DownLoadFileServlet extends HttpServlet {
  
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
       
        String password = request.getParameter("password");
        String filepath = "C:\\Users\\lucifer\\Desktop\\aa.pdf";

        if((!checkPassword(password))||password == null){
             PrintWriter out = response.getWriter();
             response.setContentType("text/html;charset=UTF-8");
             out.println("<head><title>錯誤信息</title></head>");
             out.println("<h1 align = 'center'>您輸入的密碼不正確!</h1><hr>");
        }
        else{
             long totalsize = 0;
             File f = new File(filepath);
             long filelength = f.length();
             byte[] b = new byte[1024];
             DataInputStream dis = new DataInputStream(new FileInputStream(f));
             response.setHeader("Content-dispostion", "attachment;filename=" + filepath);
             response.setContentType("application/pdf");
             String filesize = Long.toString(filelength);
             response.setHeader("Content-Length", filesize);
             ServletOutputStream servletOut = response.getOutputStream();
             while(totalsize < filelength){
                  totalsize += 1024;
                  if(totalsize > filelength){
                       byte[] leftpart = new byte[1024 - (int)(totalsize - filelength)];
                       dis.readFully(leftpart);
                       servletOut.write(leftpart);
                  }
                  else{
                       dis.readFully(b);
                       servletOut.write(b);
                  }
             }
             servletOut.close();
        }
    }

    private boolean checkPassword(String s){
         String[] passwords = {"lucifer","lxh"};
         boolean flag = false;
         for(int i = 0;i < passwords.length;i++){
              if(s.equals(passwords[i])){
                   flag = true;
                   break;
              }
         }
         return flag;
    }

    public void init(ServletConfig cfg)
         throws ServletException{
              super.init(cfg);
         }

    public void destroy(){
         super.destroy();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

}

如對本文有疑問,請?zhí)峤坏浇涣髡搲瑥V大熱心網(wǎng)友會為你解答??! 點(diǎn)擊進(jìn)入論壇

發(fā)表評論 (873人查看,0條評論)
請自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動的言論。
昵稱:
最新評論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時(shí)間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號