当前位置: 代码迷 >> Web前端 >> servlet上载文件
  详细解决方案

servlet上载文件

热度:128   发布时间:2012-10-07 17:28:51.0
servlet下载文件
下载文件的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);
    }
}
  相关解决方案