[转]AppFuse中的SiteMesh问题

Posted on 2005-06-05 17:37  数码民工  阅读(1487)  评论(0编辑  收藏  举报
http://www.blogbus.com/public/tb.php/1216085
 因为AppFuse中使用的默认的JSP模板为SiteMesh,默认情况下,所有的请求都会被SiteMesh所装饰,所以我要实现点击文件链接,直接下载文件时却得不到正确格式的文件,得到的是一个html文件,即使制定了下载文件的类型:如:application/msexcel或者其他的类型都不行,我分析是因为,我首先向输出流中写了些东西,后来被SiteMesh使用getWriter方法给冲掉了,所以得到的是默认的网页文件,我折腾了2天,痛不欲生,刚刚解决掉这个问题,解决方法如下:

1.将下载文件的jsp放到一个特殊的目录下,我的放在download目录,这样可以区分需要被sitemesh装饰和不需要被它装饰的文件。

2.配置sitemesh的decorators.xml:
<decorators defaultdir="/decorators">
    <excludes>
        <pattern>/download/*</pattern>
    </excludes>
</decorators>

这个配置指明download下的文件不需要被sitemesh装饰。



我的下载文件的页面代码是试验代码,稍加修改就可以正式使用,代码如下:
<%@ page import= "java.io.*,java.net.*" %><%
    try{
        //取得虚拟的路径
        String fn = "attachment; filename=a.xls";  //必须改为UniCode编码的字符串
        System.out.println(fn);
        //把标题、内容写到输出流中
        response.setHeader("Content-Disposition", new String(fn.getBytes("GB2312"),
                                        "ISO-8859-1"));
        createOutput( response.getOutputStream(),"c:\\ltf.xls");
    }catch( Exception ee ){
        ee.printStackTrace();
    }
%><%!
public void createOutput( OutputStream out,String realpath ) throws IOException {
    int b;
    BufferedInputStream  m_input =
                new BufferedInputStream( new  FileInputStream(realpath) );
    while( (b = m_input.read()) != -1 ){
        out.write(b);
    }
    m_input.close();
    out.flush();
    out.close();
}
%>
<%--
  // 得到文件名字和路径
  String filename = request.getParameter("zipfilename");
  //String jsppath = "";             getServletConfig().getServletContext().getRealPath("") +
   String filepath = "c:\\ltf.xls";        //jsppath.substring(0,jsppath.lastIndexOf("")) + "reports\\";
  System.out.println("---------------=================" + filepath);
  java.io.FileInputStream fileInputStream = null;
  //out.println(filepath);
  // 设置响应头和下载保存的文件名
  response.setHeader("Content-Disposition",
  "attachment; filename=" + "a.xls");
  response.setContentType("application/msexcel");
 
  // 打开指定文件的流信息
  try{
  fileInputStream =
   new java.io.FileInputStream(filepath);
  
  // 写出流信息
  int i;
  while ((i=fileInputStream.read()) != -1) {
   out.write(i);
  }
  fileInputStream.close();
  out.close();
  System.out.println("---OK---------------");
  
  }catch(Exception ee){
    ee.printStackTrace();
  }
--%>

Copyright © 2024 数码民工
Powered by .NET 8.0 on Kubernetes