Servlet实现文件下载

sss

在页面插入如标签

<a href="Download?path=2.txt">下载</a>

新建名为Download的servlet

package com.cgyue;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Download extends HttpServlet {

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //服务器相对路径
        String path= request.getParameter("path");
        //服务器绝对路径(下载文件存放的目录)
        path = this.getServletContext().getRealPath("/")+"sourceFile"+File.separator+path;
        //检查文件是否存在
        File file = new File(path);
        if(!file.exists()){
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().println("指定文件不存在");
            return;
        }

        String fileName = path.substring(path.lastIndexOf("\\"));
        //写流文件到前端浏览器
        ServletOutputStream sos = response.getOutputStream();
        response.setHeader("Content-disposition", "attachment;filename="+fileName);
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try{
            bis = new BufferedInputStream(new FileInputStream(path));
            bos = new BufferedOutputStream(sos);
            byte[] buff = new byte[2048];
            int bytesRead;
            while(-1 != (bytesRead = bis.read(buff,0,buff.length))){
                bos.write(buff, 0, bytesRead);
            }
        }catch(IOException e){
            throw e;
        }finally{
            if(bis != null){
                bis.close();
            }
            if(bos != null){
                bos.close();
            }
        }
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doGet(request, response);
    }

}

 

posted @ 2013-07-26 13:33  cgyue  阅读(299)  评论(0编辑  收藏  举报
cgyue个人博客