您现在的位置: 365建站网 > 365学习 > Servlet实现文件下载

Servlet实现文件下载

文章来源:365jz.com     点击数:818    更新时间: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";
    }

}

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛


发表评论 (818人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码: 点击我更换图片
最新评论
------分隔线----------------------------