JSP文件下载--流方式

<%@page language="java" contentType="application/x-msdownload" import="java.io.*,java.net.*" pageEncoding="gbk"%><%
    
    response.reset();//可以加也可以不加
    response.setContentType("application/x-download");//设置为下载application/x-download
    // /../../退WEB-INF/classes两级到应用的根目录下去,注意Tomcat与WebLogic下面这一句得到的路径不同,WebLogic中路径最后没有/
    ServletContext context = session.getServletContext();
	String realContextPath = context.getRealPath("")+"\\plan\\计划数据模板.xls"; 
    String filenamedisplay = "计划数据模板.xls";
    filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8");
    response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);
	
    OutputStream output = null;
    FileInputStream fis = null;
    try
    {
        output  = response.getOutputStream();
fis = new FileInputStream(realContextPath);
        byte[] b = new byte[1024];
        int i = 0;

        while((i = fis.read(b)) > 0)
        {
            output.write(b, 0, i);
        }
        output.flush();
    }
    catch(Exception e)
    {
        System.out.println("Error!");
        e.printStackTrace();
    }
    finally
    {
        if(fis != null)
        {
            fis.close();
            fis = null;
        }
        if(output != null)
        {
            output.close();
            output = null;
        }
    }
%>

你可能感兴趣的:(java,tomcat,jsp,Web,weblogic)