?? jsp文件下載及出現(xiàn)getoutputstream() has already been called for ___的解決方法.htm
字號(hào):
<TR vAlign=top>
<TD width="99%">
<TABLE
style="TABLE-LAYOUT: fixed; WORD-BREAK: break-all; WORD-WRAP: break-word"
width="100%">
<TBODY>
<TR>
<TD><SPAN class=bright-subject><A
name=178423></A>JSP文件下載及出現(xiàn)getOutputStream() has already been
called for ...的解決方法 </SPAN></TD></TR></TBODY></TABLE><BR>提交時(shí)間: Nov
26, 2005 11:03:55 AM </TD>
<TD width="1%">
<TABLE cellSpacing=2 cellPadding=3 border=0>
<TBODY>
<TR>
<TD noWrap><SPAN style="COLOR: red">加3分</SPAN></TD>
<TD> </TD>
<TD noWrap><A title=點(diǎn)擊回復(fù)此主題
href="http://dev2dev.bea.com.cn/bbs/post!reply.jspa?forumID=121&threadID=29766&messageID=178423&rep=true">引用<!--img src="/bbs/images/reply-16x16.gif" width="16" height="16" border="0"--></A>
</TD>
<TD noWrap><A title=點(diǎn)擊回復(fù)此主題
href="http://dev2dev.bea.com.cn/bbs/post!reply.jspa?forumID=121&threadID=29766&messageID=178423">回復(fù)</A>
</TD>
<TD noWrap><A title=點(diǎn)擊給作者發(fā)站內(nèi)消息
onclick="sendMessage(1791);return false;"
href="http://dev2dev.bea.com.cn/bbs/thread.jspa?forumID=121&threadID=29766&tstart=0#">發(fā)消息</A>
</TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD class=message_content style="BORDER-TOP: #ccc 1px solid"
colSpan=2><BR>
<TABLE
style="TABLE-LAYOUT: fixed; WORD-BREAK: break-all; WORD-WRAP: break-word"
width="100%">
<TBODY>
<TR>
<TD>JSP文件下載及出現(xiàn)getOutputStream() has already been called for
this
response的解決方法<BR><BR>http://iamin.blogdriver.com/iamin/1072546.html<BR><BR>一、采用RequestDispatcher的方式進(jìn)行<BR><BR>1、web.xml文件中增加<BR>
<mime-mapping><BR>
<extension>doc</extension><BR>
<mime-type>application/vnd.ms-word</mime-type><BR>
</mime-mapping><BR><BR><BR>2、程序如下:<BR><BR><%@page
language="java" import="java.net.*"
pageEncoding="gb2312"%><BR><%<BR>
response.setContentType("application/x-download");//設(shè)置為下載application/x-download<BR>
String filenamedownload =
"/系統(tǒng)解決方案.doc";//即將下載的文件的相對(duì)路徑<BR> String
filenamedisplay =
"系統(tǒng)解決方案.doc";//下載文件時(shí)顯示的文件保存名稱<BR>
filenamedisplay =
URLEncoder.encode(filenamedisplay,"UTF-8");<BR>
response.addHeader("Content-Disposition","attachment;filename="
+ filenamedisplay);<BR>
<BR> try<BR>
{<BR>
RequestDispatcher dispatcher =
application.getRequestDispatcher(filenamedownload);<BR>
if(dispatcher !=
null)<BR>
{<BR>
dispatcher.forward(request,response);<BR>
}<BR>
response.flushBuffer();<BR>
}<BR> catch(Exception
e)<BR>
{<BR>
e.printStackTrace();<BR>
}<BR> finally<BR>
{<BR> <BR>
}<BR>%><BR><BR><BR>二、采用文件流輸出的方式下載<BR><BR>1、web.xml文件中增加<BR>
<mime-mapping><BR>
<extension>doc</extension><BR>
<mime-type>application/vnd.ms-word</mime-type><BR>
</mime-mapping><BR><BR><BR>2、程序如下:<BR><BR><%@page
language="java" contentType="application/x-msdownload"
import="java.io.*,java.net.*"
pageEncoding="gb2312"%><%<BR>
//關(guān)于文件下載時(shí)采用文件流輸出的方式處理:<BR>
//加上response.reset(),并且所有的%>后面不要換行,包括最后一個(gè);<BR>
//因?yàn)锳pplication
Server在處理編譯jsp時(shí)對(duì)于%>和<%之間的內(nèi)容一般是原樣輸出,而且默認(rèn)是PrintWriter,<BR>
//而你卻要進(jìn)行流輸出:ServletOutputStream,這樣做相當(dāng)于試圖在Servlet中使用兩種輸出機(jī)制,<BR>
//就會(huì)發(fā)生:getOutputStream() has already been called for this
response的錯(cuò)誤<BR> //詳細(xì)請(qǐng)見《More Java
Pitfill》一書的第二部分 Web層Item 33:試圖在Servlet中使用兩種輸出機(jī)制
270<BR>
//而且如果有換行,對(duì)于文本文件沒有什么問題,但是對(duì)于其它格式,比如AutoCAD、Word、Excel等文件<BR>
//下載下來的文件中就會(huì)多出一些換行符0x0d和0x0a,這樣可能導(dǎo)致某些格式的文件無法打開,有些也可以正常打開。<BR><BR>
response.reset();//可以加也可以不加<BR>
response.setContentType("application/x-download");//設(shè)置為下載application/x-download<BR>
//
/../../退WEB-INF/classes兩級(jí)到應(yīng)用的根目錄下去,注意Tomcat與WebLogic下面這一句得到的路徑不同,WebLogic中路徑最后沒有/<BR>
System.out.println(this.getClass().getClassLoader().getResource("/").getPath());<BR>
String filenamedownload =
this.getClass().getClassLoader().getResource("/").getPath() +
"/../../系統(tǒng)解決方案.doc";<BR> String
filenamedisplay =
"系統(tǒng)解決方案.doc";//系統(tǒng)解決方案.txt<BR>
filenamedisplay =
URLEncoder.encode(filenamedisplay,"UTF-8");<BR>
response.addHeader("Content-Disposition","attachment;filename="
+ filenamedisplay);<BR><BR> OutputStream
output = null;<BR> FileInputStream fis =
null;<BR> try<BR>
{<BR> output =
response.getOutputStream();<BR>
fis = new
FileInputStream(filenamedownload);<BR><BR>
byte[] b = new
byte[1024];<BR> int
i = 0;<BR><BR>
while((i = fis.read(b)) >
0)<BR>
{<BR>
output.write(b, 0,
i);<BR>
}<BR>
output.flush();<BR> }<BR>
catch(Exception e)<BR>
{<BR>
System.out.println("Error!");<BR>
e.printStackTrace();<BR>
}<BR> finally<BR>
{<BR> if(fis !=
null)<BR>
{<BR>
fis.close();<BR>
fis = null;<BR>
}<BR> if(output !=
null)<BR>
{<BR>
output.close();<BR>
output = null;<BR>
}<BR>
}<BR>%><BR></TD></TR></TBODY></TABLE><BR><BR>
<HR style="COLOR: #cccccc; HEIGHT: 1px" align=left>
<TABLE
style="TABLE-LAYOUT: fixed; WORD-BREAK: break-all; WORD-WRAP: break-word"
width="100%">
<TBODY>
<TR>
<TD>
<DIV
style="PADDING-RIGHT: 4px; PADDING-LEFT: 4px; PADDING-BOTTOM: 4px; WIDTH: 96%; PADDING-TOP: 4px; TEXT-ALIGN: left">只有正確地找到問題的根本所在,才能成功地解決掉問題:)<BR>Read
a bit and take it out , then come back read some more .<BR><A
href="http://yulimin.javaeye.com/" target=_blank><BR><IMG
style="CURSOR: pointer"
onclick=javascript:window.open(this.src); src=""
onload="javascript:if(this.width>400)this.style.width=400;"><BR></IMG></A></DIV></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></SPAN><BR><SPAN
class=bright-message-list>
<TABLE class=bright-box cellSpacing=1 cellPadding=3 width="98%" align=center
border=0>
<TBODY>
<TR class=bright-even vAlign=top>
<TD width="1%">
<TABLE cellSpacing=0 cellPadding=0 width=180 border=0>
<TBODY>
<TR>
<TD><A title=""
href="http://dev2dev.bea.com.cn/bbs/profile.jspa?userID=1791"><B>YuLimin</B></A>
[版主] <BR><BR>發(fā)帖數(shù): 2,283 <BR>活躍積分: 2,472 <BR>技術(shù)積分: 342 <BR>可用幣值: 187
<BR>注冊(cè)時(shí)間: 2002-9-4 <BR>用戶狀態(tài):<FONT color=green>正常</FONT>
<BR></TD></TR></TBODY></TABLE></TD>
<TD width="99%">
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR vAlign=top>
<TD width="99%">
<TABLE
style="TABLE-LAYOUT: fixed; WORD-BREAK: break-all; WORD-WRAP: break-word"
width="100%">
<TBODY>
<TR>
<TD><SPAN class=bright-subject><A name=179308></A>Re:
JSP文件下載及出現(xiàn)getOutputStream() has already been called for
...的解決方法 </SPAN></TD></TR></TBODY></TABLE><BR>提交時(shí)間: Nov 30, 2005
3:20:11 PM </TD>
<TD width="1%">
<TABLE cellSpacing=2 cellPadding=3 border=0>
<TBODY>
<TR>
<TD vAlign=center noWrap>
<FORM name=179308 action=send!default.jspa method=post
target=_blank><INPUT type=hidden value=申請(qǐng)積分 name=subject>
<INPUT type=hidden value=YuLimin,fengw name=master> <INPUT
type=hidden
value=帖子http://dev2dev.bea.com.cn//bbs/thread.jspa?forumID=121&threadID=29766&messageID=179308申請(qǐng)積分
name=body> <INPUT type=image
src="JSP文件下載及出現(xiàn)getOutputStream() has already been called for ___的解決方法.files/sqjifen0.gif"
border=0 name=submit> </TD></FORM>
<TD> </TD>
<TD noWrap><A title=點(diǎn)擊回復(fù)此主題
href="http://dev2dev.bea.com.cn/bbs/post!reply.jspa?forumID=121&threadID=29766&messageID=179308&rep=true">引用<!--img src="/bbs/images/reply-16x16.gif" width="16" height="16" border="0"--></A>
</TD>
<TD noWrap><A title=點(diǎn)擊回復(fù)此主題
href="http://dev2dev.bea.com.cn/bbs/post!reply.jspa?forumID=121&threadID=29766&messageID=179308">回復(fù)</A>
</TD>
<TD noWrap><A title=點(diǎn)擊給作者發(fā)站內(nèi)消息
onclick="sendMessage(1791);return false;"
href="http://dev2dev.bea.com.cn/bbs/thread.jspa?forumID=121&threadID=29766&tstart=0#">發(fā)消息</A>
</TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD colSpan=2>
<TABLE cellSpacing=0 cellPadding=3 width="100%" border=0>
<TBODY>
<TR>
<TD vAlign=top width="1%"><A
href="http://dev2dev.bea.com.cn/bbs/servlet/D2DServlet/download/121-29766-179308-2306/DownloadFile.war"
target=_blank><IMG
src="JSP文件下載及出現(xiàn)getOutputStream() has already been called for ___的解決方法.files/D2DServlet"
border=0> </A></TD>
<TD width="98%"><A
href="http://dev2dev.bea.com.cn/bbs/servlet/D2DServlet/download/121-29766-179308-2306/DownloadFile.war"
target=_blank>DownloadFile.war</A> (10.4
K)<BR></TD></TR></TBODY></TABLE><BR>
<TABLE cellSpacing=0 cellPadding=3 width=400 border=0>
<TBODY>
<TR>
<TD></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD class=message_content style="BORDER-TOP: #ccc 1px solid"
colSpan=2><BR>
<TABLE
style="TABLE-LAYOUT: fixed; WORD-BREAK: break-all; WORD-WRAP: break-word"
width="100%">
<TBODY>
<TR>
<TD>修改了一下,在WebLogic上面路徑/的問題。
<BR>Tomcat與WebLogic下面這一句得到的路徑不同,WebLogic中路徑最后沒有/<BR></TD></TR></TBODY></TABLE><BR><BR>
<HR style="COLOR: #cccccc; HEIGHT: 1px" align=left>
<TABLE
style="TABLE-LAYOUT: fixed; WORD-BREAK: break-all; WORD-WRAP: break-word"
width="100%">
<TBODY>
<TR>
<TD>
<DIV
style="PADDING-RIGHT: 4px; PADDING-LEFT: 4px; PADDING-BOTTOM: 4px; WIDTH: 96%; PADDING-TOP: 4px; TEXT-ALIGN: left">只有正確地找到問題的根本所在,才能成功地解決掉問題:)<BR>Read
a bit and take it out , then come back read some more .<BR><A
href="http://yulimin.javaeye.com/" target=_blank><BR><IMG
style="CURSOR: pointer"
onclick=javascript:window.open(this.src); src=""
onload="javascript:if(this.width>400)this.style.width=400;"><BR></IMG></A></DIV></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></SPAN><SPAN
class=bright-message-list>
<TABLE class=bright-box cellSpacing=1 cellPadding=3 width="98%" align=center
border=0>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -