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.getAttr ibute(LFCo nstants.CH ANNEL);
if (channel != null)
{
// Object which contains the File in InputStream format
objAttach = (certclsAttachment)channel .getAttrib ute("ATTAC HMENT");
}
// File from the database as InputStream
InputStream is = objAttach.getFileStream();
int intBufferSize = objAttach.getBufferSize();
String strAttName = objAttach.getAttName();
response.setContentType(ob jAttach.ge tContentTy pe());
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
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.getAttr
if (channel != null)
{
// Object which contains the File in InputStream format
objAttach = (certclsAttachment)channel
}
// File from the database as InputStream
InputStream is = objAttach.getFileStream();
int intBufferSize = objAttach.getBufferSize();
String strAttName = objAttach.getAttName();
response.setContentType(ob
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
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(121 6525,EOTR, /EOTR)] Servlet failed with IOExceptio
n
java.net.ProtocolException : Exceeded stated content-length of: '646' bytes
at weblogic.servlet.internal. ServletOut putStreamI mpl.checkC L(ServletO utputStrea mImpl.java :217)
at weblogic.servlet.internal. ServletOut putStreamI mpl.write( ServletOut putStreamI mpl.java:1 67)
at java.io.OutputStream.write (OutputStr eam.java:5 6)
at jsp_servlet._war._eotr._js p.__certvi ewattachme nt._jspSer vice(__cer tviewattac hment.java :142)
at weblogic.servlet.jsp.JspBa se.service (JspBase.j ava:27)
at weblogic.servlet.internal. ServletStu bImpl.invo keServlet( ServletStu bImpl.java :265)
at weblogic.servlet.internal. ServletStu bImpl.invo keServlet( ServletStu bImpl.java :200)
at weblogic.servlet.internal. RequestDis patcherImp l.forward( RequestDis patcherImp l.java:242 )
at eotr.lightframe.Dispatcher .doPost(Di spatcher.j ava:312)
at eotr.lightframe.Dispatcher .doGet(Dis patcher.ja va:420)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 740)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 853)
at weblogic.servlet.internal. ServletStu bImpl.invo keServlet( ServletStu bImpl.java :265)
at weblogic.servlet.internal. ServletStu bImpl.invo keServlet( ServletStu bImpl.java :200)
at weblogic.servlet.internal. WebAppServ letContext .invokeSer vlet(WebAp pServletCo ntext.java :2546)
at weblogic.servlet.internal. ServletReq uestImpl.e xecute(Ser vletReques tImpl.java :2260)
at weblogic.kernel.ExecuteThr ead.execut e(ExecuteT hread.java :139)
at weblogic.kernel.ExecuteThr ead.run(Ex ecuteThrea d.java:120 )
>
And if I use without setContentLength, the result is same as before.
Thanks for the quick response. When I tried to use "setContentLength" :
response.setContentLength(
It gave me folowing error:
<Feb 25, 2003 11:55:15 AM EST> <Error> <HTTP> <[WebAppServletContext(121
n
java.net.ProtocolException
at weblogic.servlet.internal.
at weblogic.servlet.internal.
at java.io.OutputStream.write
at jsp_servlet._war._eotr._js
at weblogic.servlet.jsp.JspBa
at weblogic.servlet.internal.
at weblogic.servlet.internal.
at weblogic.servlet.internal.
at eotr.lightframe.Dispatcher
at eotr.lightframe.Dispatcher
at javax.servlet.http.HttpSer
at javax.servlet.http.HttpSer
at weblogic.servlet.internal.
at weblogic.servlet.internal.
at weblogic.servlet.internal.
at weblogic.servlet.internal.
at weblogic.kernel.ExecuteThr
at weblogic.kernel.ExecuteThr
>
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());
response.setContentLength(
response.getOutputStream ().write (bout.toByteArray());
can you show use the code of certclsAttachment? thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks allahabad,
It wa shte perfect answer !
Thanks again!
JKB88
It wa shte perfect answer !
Thanks again!
JKB88
You are welcome.
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte buffer[] = new byte[intBufferSize];
while ((length = is.read(buffer)) != -1)
{
out.write(buffer,0,length)
}
is.close();
response.setContentType("a
response.setContentLength(
response.getOutputStream ().write (out.toByteArray());
response.getOutputStream()
response.getOutputStream()