随笔 - 6  文章 - 129  trackbacks - 0
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(14)

随笔档案(6)

文章分类(467)

文章档案(423)

相册

收藏夹(18)

JAVA

搜索

  •  

积分与排名

  • 积分 - 815432
  • 排名 - 49

最新评论

阅读排行榜

评论排行榜

朋友在處理Jsp時, 希望能夠將產生好的excel檔案能夠下載到Client 端
但要將產生好的file利用 SmartUpload 下載時, 發生以下問題
getOutputStream() has already been called for this response
後來找到了解決方法如下:
<範例>
@page contentType="text/html; charset=big5" 
@page session
="true" errorPage="error.jsp"
 
@page 
import="com.jspsmart.upload.*"
 
String ret 
= request.getParameter("ret"
);
if (ret != null
) { 
ret 
=
 java.net.URLDecoder.decode(ret);
ret 
= new String(ret.getBytes("8859_1"),"Big5"
);
}
else ret = ""
;
String file 
= request.getParameter("file"
);
if (file != null
) {
file 
=
 java.net.URLDecoder.decode(file);
file 
= new String(file.getBytes("8859_1"),"Big5"
);
else file = ""
;
// 新建一個SmartUpload對象 

SmartUpload su1 = new SmartUpload();
// 初始化

su1.initialize(pageContext);
//
 設定contentDisposition為null以禁止瀏覽器自動打開文件,
//
保證點擊鏈接後是下載文件。若不設定,則下載的文件擴展名為 
//
doc時,瀏覽器將自動用word打開它。擴展名為pdf時, 
//瀏覽器將用acrobat打開。

su1.setContentDisposition(null);
// 下載文件

su1.downloadFile(file);

問題原因:Tomcat首先執行.jsp, Tomcat準備好session, out等object。 而在< % ... % >段中,HttpServerletResponse的getOutputStream()方法已被呼叫。但在JSP規中定義此方法只能被使用一 次,這樣在產生out時會在使用一次, 因此會出錯。
網路上建議方法: 不要使用Jsp, 改使用Servlet就不會有此問題
後來有人回應在最後加入兩行
out.clear();
out = pageContext.pushBody();
果然解決了這個問題!!


posted on 2009-07-22 17:14 Ke 阅读(866) 评论(0)  编辑  收藏 所属分类: exception

只有注册用户登录后才能发表评论。


网站导航: