?
Solved

Wrting InputString to 'response' by setting setContentType

Posted on 2003-02-25
7
Medium Priority
?
1,078 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:JKB88
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 8017721
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();
0
 

Author Comment

by:JKB88
ID: 8018018
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.
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8018058
hrmm... you are doing this right:

response.setContentLength(bout.size());
response.getOutputStream ().write (bout.toByteArray());
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:kennethxu
ID: 8019509
can you show use the code of certclsAttachment? thanks.
0
 
LVL 3

Accepted Solution

by:
allahabad earned 400 total points
ID: 8022263
I beleive you can not display it in JSP, you will have to use servlet for this. For ex. you can test this servlet.

import java.util.*;
 import java.io.*;
 import javax.servlet.http.*;
 import javax.servlet.*;
 import java.sql.*;

 public class DisplayImageServlet extends HttpServlet {

   public void doGet(HttpServletRequest req, HttpServletResponse res)
   throws ServletException, IOException{

         res.setContentType("image/gif");
         try {


               String url = "jdbc:oracle:thin:@localhost:1521:orcl";
               String jdbcDriver = "oracle.jdbc.driver.OracleDriver";


               Class.forName(jdbcDriver);
               Connection con = java.sql.DriverManager.getConnection(url,"scott","tiger");
               Statement stmt = con.createStatement();
               // put you sql here, in my case image field had BLOB
               String query = "SELECT image FROM file_test";
               ResultSet rs1 = stmt.executeQuery(query);
               if (rs1.next()){
                  ServletOutputStream o= res.getOutputStream();
                  InputStream in=rs1.getBinaryStream(1);
                 
                  byte[] bt = new byte [1000];
                  for(int i = in.read(bt); i != -1; )
                  {
                     o.write(bt);
                     in.read(bt);
                  }
                  o.flush();
                  o.close();

              }
              con.close();
             
             
             



         }
         catch (Exception e){
              e.printStackTrace();
}

   }
 }
0
 

Author Comment

by:JKB88
ID: 8028591
Thanks allahabad,

It wa shte perfect answer !

Thanks again!

JKB88
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8028778
You are welcome.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month8 days, 21 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question