dcsimg
IllegalStateException - getOutputStream() has already been called
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Ranga_Raj
Posted On:   Friday, November 8, 2002 06:02 PM

Hi all, I have to write a download code in JSP. I used getOutputStream() of response object in JSP. Below is the code for download a file in JSP. %> <%@ page import="java.util.*, java.io.*" %> <%@ page language="java" session="false" contentType="text/html; charset=8859_1" %> <% //read the file name. File fFile = new File ("D:/Ibs/outdir/batchres.conf"); String stFileName = "batchres.conf"; //the content type set as excel response.setContentType ("application/excel"); //the header and also the Nameis set by which user will be prompted    More>>

Hi all,

I have to write a download code in JSP.
I used getOutputStream() of response object in JSP. Below is the code for download a file in JSP.

%>
<%@ page import="java.util.*,
java.io.*"
%>
<%@ page language="java"
session="false"
contentType="text/html; charset=8859_1"
%>
<%
//read the file name.
File fFile = new File ("D:/Ibs/outdir/batchres.conf");
String stFileName = "batchres.conf";
//the content type set as excel
response.setContentType ("application/excel");
//the header and also the Nameis set by which user will be prompted to save
response.setHeader ("Content-Disposition", "attachment;filename=""+stFileName+""");

//Open an input stream to the file and post the file contents thru the
//servlet output stream to the client m/c
InputStream isStream = null;
ServletOutputStream sosStream = null;
try
{
//response.flushBuffer();
isStream = new FileInputStream(fFile);
sosStream = response.getOutputStream();
int ibit = 256;
while ((ibit) >= 0)
{
ibit = isStream.read();
sosStream.write(ibit);
}

}
catch (IOException ioeException)
{
}
sosStream.flush();
sosStream.close();
isStream.close();
%>


If run this code in Tomcat i am getting following error..

ΓΏ

Error: 500


Location: /imu/jsp/ibUTLCmnDownloadView.jsp

Internal Servlet Error:

			java.lang.IllegalStateException: getOutputStream() has already been called
			
at org.apache.tomcat.facade.HttpServletResponseFacade.getWriter(Unknown Source)
at org.apache.jasper.runtime.JspWriterImpl.initOut(Unknown Source)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(Unknown Source)
at jsp.ibUTLCmnDownloadView_12._jspService(ibUTLCmnDownloadView_12.java, Compiled Code)
at org.apache.jasper.runtime.HttpJspBase.service(Unknown Source)
at javax.servlet.http.HttpServlet.service(HttpServlet.java)
at org.apache.tomcat.facade.ServletHandler.doService(Unknown Source)
at org.apache.tomcat.core.Handler.invoke(Unknown Source)
at org.apache.tomcat.core.Handler.service(Unknown Source)
at org.apache.tomcat.facade.ServletHandler.service(Unknown Source)
at org.apache.tomcat.facade.RequestDispatcherImpl.doForward(Unknown Source)
at org.apache.tomcat.facade.RequestDispatcherImpl.forward(Unknown Source)
at JP.co.Hitachi.soft.IBS.Common.Servlet.ibUTLCmnDownloadScrGenServlet.doPost(ibUTLCmnDownloadScrGenServlet.java:75)
at javax.servlet.http.HttpServlet.service(HttpServlet.java)
at javax.servlet.http.HttpServlet.service(HttpServlet.java)
at org.apache.tomcat.facade.ServletHandler.doService(Unknown Source)
at org.apache.tomcat.core.Handler.invoke(Unknown Source)
at org.apache.tomcat.core.Handler.service(Unknown Source)
at org.apache.tomcat.facade.ServletHandler.service(Unknown Source)
at org.apache.tomcat.core.ContextManager.internalService(Unknown Source)
at org.apache.tomcat.core.ContextManager.service(Unknown Source)
at org.apache.tomcat.modules.server.Http10Interceptor.processConnection(Unknown Source)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(Unknown Source)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(Unknown Source)
at java.lang.Thread.run(Thread.java:479)




Can any one help me out?
-Rangaraj    <<Less

Re: IllegalStateException - getOutputStream() has already been called

Posted By:   michael_dean  
Posted On:   Friday, November 8, 2002 10:40 PM

Don't know if this will help--your code worked as is on my system (J2SDK 1.4.1, Tomcat 4.1.12, Linux)--but you're setting the content-type twice, and to two different values. In the page directive, you specify contentType="text/html; charset=8859_1", and then in the scriptlet, you do a response.setContentType ("application/excel");. Try changing the one in the page directive and deleting the one in the scriptlet. The servlet container may be calling getOutputStream() when it sees the text MIME type so it can prepare the out built-in variable.



Also, according to the J2EE design patterns, JSP's should only be used to produce text output. Any binary output (such as Excel files) should be produced with servlets--otherwise, the JSP becomes one big scriptlet (like this one).



If you still want to do this with a JSP, you might want to take out your try ... catch block since you're not doing anything with it. Doing so will allow you to let the servlet container handle the errors (i.e. specify error pages in the web application deployment descriptor). Either that, or at least put the close() and flush()calls in it since they can throw IOExceptions, too. :)



Finally, you should never close the servlet's output stream. Leave that up to the servlet container.

About | Sitemap | Contact