• Post Reply Bookmark Topic Watch Topic
  • New Topic

probelms with response.getOutputStream  RSS feed

 
nandkishor rao
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
My servlet is reading a zip file and writing it to reponse
using ServletOutputStream. But while the Servlet gets triggered its giving me this exception and the funny thing is i am not calling getWriter() on response anywhere. If any of you having any suggestions please reply.

java.lang.IllegalStateException: strict servlet API: cannot call getOutputStream() after getWriter()at weblogic.servlet.internal.ServletResponseImpl.getOutputStream(ServletResponseImpl.java:253)
at com.bt.oss.trengtd.AfmServlet.doGet(AfmServlet.java:64)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:225)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:127)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:272)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:165)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3153)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:1973)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:1880)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1310)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:207)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:179)
 
John Eric Hamacher
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be helpful to post your servlet code.
 
saqib sarwar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest check few things.

1. setContentType for response before writing.
2. better collect whole file in a byte array and then write it at once to response.
3. check your code that it should not call getWriter and getOutputStream methods both in same method.
 
nandkishor rao
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is the Servlet code please let me know if there are any suggestions

public class AfmServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 7467039855104622536L;
// private static final Log log = LogFactory.getLog(AfmServlet.class);
/**
* @param request The request recieved from the client
* @param response The response to the client
* @throws ServletException
* @throws IOException
*/

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html");
//Get the request parameters
String nni = request.getParameter("nni");
String msg = nni+" nni received";
response.sendError(HttpServletResponse.SC_OK, msg);
response.setHeader("nni", "0123456");
response.setHeader("resultCount","5");
// log.debug("Response Headers set");
// File afm_0123456 = null;
ServletOutputStream out = null;
try {
// File f = new File("C:/Documents and Settings/148034/Desktop/new.doc");

String baseName = "afm_0123456.zip";

String fileLocation = findFile(baseName);
File f = new File(fileLocation);
FileInputStream istr = new FileInputStream(fileLocation);
GZIPInputStream ip = new GZIPInputStream(istr);
// log.debug("FileInputStream");
BufferedInputStream bstr = new BufferedInputStream( ip ); // promote
// log.debug("BufferedInputStream");
int size = (int) f.length(); // get the file size (in bytes)
byte[] data = new byte[size]; // allocate byte array of right size
bstr.read( data, 0, size ); // read into byte array
// log.debug("DATA READ");
bstr.close();
// log.debug("BufferedInputStream CLOSED");
response.setContentType("text/html");
//
out = response.getOutputStream();
// log.debug("getOutputStream");
out.write(data);
// log.debug("WRITTEN DATA");
out.flush();
// log.debug("FLUSHED DATA");
out.close();
// afm_0123456 = new File( "afm_0123456.zip" );
// if (afm_0123456.exists()) {
// response.setContentType("text/html");
// RandomAccessFile raf = new RandomAccessFile( afm_0123456, "r" );
// response.setContentLength((int)raf.length());
// out = response.getOutputStream();
// byte [] loader = new byte [ (int) raf.length() ];
// while ( (raf.read( loader )) > 0 ) {
// out.write( loader );
// }
// }else {
// System.out.println("Aborting: File doesn't exists, " + afm_0123456 + ", " + afm_0123456 + File.separator + afm_0123456 +
// "abspath: " + afm_0123456.getAbsolutePath() );
// }
// log.debug("GOING TO DISPATCH IT TO SERVLET");
RequestDispatcher requestdispatcher = request.getRequestDispatcher("/RcuAfmCallbackServlet");
requestdispatcher.forward(request,response);
} catch (IOException ioe) {
// log.debug("Unable to open file "); ioe.printStackTrace();
} finally {
if (out != null) {
out.flush();
out.close();
}
}

}

/**
* @param request The request recieved from the client
* @param response The response to the client
* @throws ServletException
* @throws IOException
*/

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//Call the doGet() method
doGet(request, response);
}
private String findFile(String baseName) {
String absoluteFilename = Utilities.getFileAsPathOrUrl(baseName);
if (absoluteFilename.equals(baseName)) {
absoluteFilename = "C:/btp/Transfer_Engineering_Diagnostics/Construction/Java/project-web/WEB-INF/src/com/bt/oss/trengtd/" + baseName;
}
return absoluteFilename;
}
}
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As we cannot call getParameter and getInputStream in the same program , this might be because of the same thing .We cannot call getWriter and getOutputstream in the same program.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!