Re: How to display a jpeg msql Blob field

Adrian Janssen <[email protected]>
Newsgroups gmane.comp.java.sun.servlet
Message-ID <[email protected]>
The servlet should dump out JUST the image binary data and NO html at all -
as you have set the content type to "image/jpeg". This tells the browser to
expect the raw binary data of an image.

In general how this would be done ia that there are two servlets. The main
one that renders out the the html of the table includeing an <img> tag that
has as its source attribute the url of a second servlet (possibly with
parameters), and this second servlet does what you seem to be attempting in
your code snippet: render out just the raw binary of the image.

Cheers
Adrian

> -----Original Message-----
> From: Bob Prah [SMTP:[email protected]]
> Sent: 07 April 2003 04:38
> To:   [email protected]
> Subject:      How to display a jpeg msql Blob field
>
> Hi to All,
>
> I am having difficulties trying to display a jpeg Blob field from a mysql
> table in a servlet. All other fields from this table render their text
> fields apart from this particular jpeg Blob field - it'll simply not
> display anything at all.
>
> Has anyone dealt with with a similar situation using mysql and how?
>
> Will be appreciate any help.
>
>
> Thank you,
>
> Bob.
>
> Here is snippet from my code:
> <<<<
> .....
>
> Connection con;
> ResultSet res=null;
>
> public void service(ServletRequest request, ServletResponse response)
>     throws ServletException, java.io.IOException {
>
>       response.setContentType("image/jpeg");
>       PrintWriter out = response.getWriter();
>
> try {
>
>             stmt = con.createStatement();
>             res=stmt .executeQuery(
>             "SELECT textField1,textField2,imageField1 FROM tech_bks ORDER
> BY id ;");
>             out.println("<table border=0 cellpadding=5 cellspacing=2
> align=center width=970>");
>             InputStream ipsrt =null;
>             while (res.next()){
>                 blob = res.getBlob("imageField1");
>                 out.println("<tr><td width=40% >" +
> res.getString("textField1"));
>
>                 out.print("</td><td width=20% align=left>" +
> res.getString("textField2")+ "</td><td width=40% align=left>");
>
>
>                  ipsrt =  blob.getBinaryStream();
>                  try{
>                     while(IsIpsrt(ipsrt)){
>                         out.print(ipsrt.read());
>                     }
>                     out.print("</td></tr>"  );
>                 }
>
>                  catch (IOException et){}
>             }
>             out.println("</table>");
>             con.close();
>             out.close();
>         }
>         catch (SQLException e4) {}
> }
> private boolean IsIpsrt(InputStream input){
>         String dist="";
>         dist = input.toString();
>         if( dist.equalsIgnoreCase(""))
>             return true ;
>         else return false;
>     }
> ......
>
> <<<<
--

It is the strict policy of Truworths that its e-mail facility and all
e-mail communications emanating therefrom, should be utilised for
business purposes only and should conform to high professional and
business standards.   Truworths has stipulated certain regulations in
terms whereof strict guidelines relating to the use and content of
e-mail communications are laid down. The use of the Truworths e-mail
facility is not permitted for the distribution of chain letters or
offensive mail of any nature whatsoever.   Truworths hereby distances
itself from and accepts no liability in respect of the unauthorised
use of its e-mail facility or the sending of e-mail communications
for other than strictly business purposes.   Truworths furthermore
disclaims liability for any  unauthorised instruction for  which
permission was not granted.    Truworths Limited accepts no liability
for any consequences arising from or as a result of reliance on this
message unless it is in respect of bona fide Truworths business for
which proper authorisation has been granted.

Any recipient of an unacceptable communication, a chain letter or
offensive material of any nature is requested to notify the Truworths
e-mail administrator ([email protected]) immediately in order that
appropriate action can be taken against the individual concerned.

___________________________________________________________________________
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.