欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

servlet下载文件 Servlet 

程序员文章站 2022-05-24 09:34:12
...
下载文件的servlet(很简单的)

减小字体增大字体

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

public class  Download extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        try
        {
            //这里你可以做些其他的事情

            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment; filename=\"你要在保存窗口中显示的保存文件名\"");
               
            ServletOutputStream out = response.getOutputStream();
            BufferedReader br=new BufferedReader(new FileReader(要下载的的文件名));
           
            String line=br.readLine();
            while (line!=null)
            {
                out.write(line.getBytes());
                out.println();
                line=br.readLine();
            }
            out.close();
            br.close();
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        doGet(request, response);
    }
}
相关标签: Servlet