Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JSP Interest »

An issue with response.getOutputStream()

siva

2004-09-21

Replies:

Hello all,
  I am getting Illegal State Exception while using response.getOutputStream() in my JSP...Its executing fine..But at the end I am getting java.lang.IllegalStateException. I am using this response.getOuputStream() to write images to a HTML file. Thanks in advance.

My code is as follows:

<%@(protected)
import="java.io.*,java.util.*"session="true"%><jsp:useBean
id="mySmartUpload"scope="page"class="com.jspsmart.upload.SmartUpload"/><%String name=(String)session.getAttribute("userid");
String type=request.getParameter("type");
String user=request.getParameter("user");
String
filePath="/home/"+user+"/email/attachments/"+request.getParameter("name");
StringTokenizer stk=new StringTokenizer(filePath,"/");
    while(stk.hasMoreElements()){
          String home=stk.nextToken();
          if(!home.equals("home")){
          log("The base directory is wrong, going to index.jsp");
          response.sendRedirect("/index.jsp");
          return;
          }
          String user1=stk.nextToken();
          String email=stk.nextToken();
          if(!email.equals("email")){
          log("The directory after username is wrong, going to index.jsp");
          response.sendRedirect("/index.jsp");
          return;
          }
          String attachments=stk.nextToken();
          if(!attachments.equals("attachments")){
          log("The attachments directory is wrong, going to index.jsp");
          response.sendRedirect("/index.jsp");
          return;
          }
          break;
    }
log("Path is " + filePath);
File f=new File(filePath);
String getPath=f.getPath();
//out.flush();
ServletOutputStream stream=null;
BufferedInputStream bif=null;
response.reset();
if( request.getParameter("type").equals("gif") )
    response.setContentType("image/gif");
else
    response.setContentType("image/jpg");
//response.setContentType("application/x-filler");
try{
stream = response.getOutputStream();
bif = new BufferedInputStream(new FileInputStream(filePath));
}catch(Exception e){log("Exception occurred in FileDownloader.jsp , as
file does not exists" + e);
response.sendRedirect("/index.jsp");
return;
}
byte b[]=new byte[1024];
int cache=bif.read(b,0,1024);
int tot=cache;
while (cache != -1)
{
    stream.write(b,0,cache);
    //out.write(b,0,cache);
    cache = bif.read(b,0,1024);
     tot+=cache;
}
if(bif!=null)bif.close();
if(stream!=null) stream.close();
log("====================>End of FileDownloader.jsp<=================");
%>

===========================================================================
To unsubscribe: mailto listserv@(protected)".
For digest: mailto listserv@(protected)".

Some relevant archives, FAQs and Forums on JSPs can be found at:

http://java.sun.com/products/jsp
http://archives.java.sun.com/jsp-interest.html
http://forums.java.sun.com
http://www.jspinsider.com
©2008 junlu.com - Jax Systems, LLC, U.S.A.