Link to home
Start Free TrialLog in
Avatar of JKB88
JKB88

asked on

Wrting InputString to 'response' by setting setContentType

HI !

I am storing a file into database -Oracle 8i blob field. Then I retrieve them and write them to the 'response' on a JSP. The code is something like this :

     String mainContent = "";
     certclsAttachment objAttach = null;

     
     LFChannel channel = (LFChannel)session.getAttribute(LFConstants.CHANNEL);    

     if (channel != null)
     {

// Object which contains the File in InputStream format

          objAttach = (certclsAttachment)channel.getAttribute("ATTACHMENT");
     }

// File from the database as InputStream      
     InputStream is           = objAttach.getFileStream();
     int intBufferSize      = objAttach.getBufferSize();
     String strAttName     = objAttach.getAttName();
     
     response.setContentType(objAttach.getContentType());
     OutputStream os = response.getOutputStream();
     
     int length = 0;
     
     byte buffer[] = new byte[intBufferSize];    
     
     while ((length = is.read(buffer)) != -1)
     {
          os.write(buffer,0,length);
     }
     
     is.close();
     
     os.flush();
     os.close();

But this does not show the file in IE5 ie. doesn't show the word documents in the format, neither it shows the images stored in the database.

Rather than writing it to response object , if I write it to a FileOutputStream and store it on the user's machine, it retrives the document. This means the InputStram from the database doesn't have any problem. Writing it to response is creating all the problems. Any idea what could be the reason?

If i setContentType as "application/msword" then it opens the page in word and doesn't show the word document embedded in IE5.0. Any idea why it is so?

Pls. respond as soon as possible.

Thanks and Regards,

JKB88
Avatar of bobbit31
bobbit31
Flag of United States of America image

try using a ByteArrayOutputStream:

ByteArrayOutputStream out = new ByteArrayOutputStream();
byte buffer[] = new byte[intBufferSize];    
     
while ((length = is.read(buffer)) != -1)
{
     out.write(buffer,0,length);
}
is.close();

response.setContentType("application/msword");
response.setContentLength(out.size());
response.getOutputStream ().write (out.toByteArray());
response.getOutputStream().flush();
response.getOutputStream().close();
Avatar of JKB88
JKB88

ASKER

Hi bobbit!

Thanks for the quick response. When I tried to use "setContentLength" :
response.setContentLength(bout.size());

It gave me folowing error:

<Feb 25, 2003 11:55:15 AM EST> <Error> <HTTP> <[WebAppServletContext(1216525,EOTR,/EOTR)] Servlet failed with IOExceptio
n
java.net.ProtocolException: Exceeded stated content-length of: '646' bytes
        at weblogic.servlet.internal.ServletOutputStreamImpl.checkCL(ServletOutputStreamImpl.java:217)
        at weblogic.servlet.internal.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:167)
        at java.io.OutputStream.write(OutputStream.java:56)
        at jsp_servlet._war._eotr._jsp.__certviewattachment._jspService(__certviewattachment.java:142)
        at weblogic.servlet.jsp.JspBase.service(JspBase.java:27)
        at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:265)
        at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:200)
        at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:242)
        at eotr.lightframe.Dispatcher.doPost(Dispatcher.java:312)
        at eotr.lightframe.Dispatcher.doGet(Dispatcher.java:420)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:265)
        at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:200)
        at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:2546)
        at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2260)
        at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:139)
        at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:120)
>

And if I use without setContentLength, the result is same as before.
hrmm... you are doing this right:

response.setContentLength(bout.size());
response.getOutputStream ().write (bout.toByteArray());
can you show use the code of certclsAttachment? thanks.
ASKER CERTIFIED SOLUTION
Avatar of allahabad
allahabad

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JKB88

ASKER

Thanks allahabad,

It wa shte perfect answer !

Thanks again!

JKB88
You are welcome.