惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
量子位
腾讯CDC
A
About on SuperTechFans
小众软件
小众软件
Microsoft Azure Blog
Microsoft Azure Blog
T
Tailwind CSS Blog
V
V2EX
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
GbyAI
GbyAI
Recent Announcements
Recent Announcements
Microsoft Security Blog
Microsoft Security Blog
博客园 - 叶小钗
罗磊的独立博客
宝玉的分享
宝玉的分享
WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
V
Visual Studio Blog
D
DataBreaches.Net
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
有赞技术团队
有赞技术团队

博客园 - 数码民工

Jabber 技 术 概 况 [收藏] 2005年的第一场暴力--《最恶都市》 appfuse中集成valuelist 揭开极端编程的神秘面纱: 测试驱动的编程 揭开极端编程的神秘面纱: 工作的首选(XP)工具 hibernate3的中文查询问题 [转]AppFuse中使用Oracle需要注意的问题 IntelliJ IDEA最佳中文字型設定 Sunjava虚拟机和Bea JRockit虚拟机垃圾收集器--gc的机制详解 使用Ant管理配置Weblogic WebLogic Server 8.1虚拟主机配置最佳实践 WebLogic管理指南[转] WebLogic Server 管理最佳实践 通用OLAP方案 什么是联机分析处理(OLAP) 绝望-《帝国的毁灭》 悲怆:IT人的一声叹息-一个程序员的自白 新的开始 反派大聚会-《半条命2》中的联合军
[转]AppFuse中的SiteMesh问题
数码民工 · 2005-06-05 · via 博客园 - 数码民工

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();
  }
--%>