Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1139
  • Last Modified:

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
0
JKB88
Asked:
JKB88
  • 2
  • 2
  • 2
  • +1
1 Solution
 
bobbit31Commented:
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
 
JKB88Author Commented:
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
 
bobbit31Commented:
hrmm... you are doing this right:

response.setContentLength(bout.size());
response.getOutputStream ().write (bout.toByteArray());
0
Technology Partners: 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!

 
kennethxuCommented:
can you show use the code of certclsAttachment? thanks.
0
 
allahabadCommented:
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
 
JKB88Author Commented:
Thanks allahabad,

It wa shte perfect answer !

Thanks again!

JKB88
0
 
allahabadCommented:
You are welcome.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now