• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

byte[] Image in jsp

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

A problem I have been wrestling with for a while and wonder if anyone have any idea how to solve it.

Im using java 1.4.2, jboss 4.0.0, mysql db and using the springwebframework.
I have an .jpg or .gif image downloaded to my mysql db as a byte[], the type in the db is BLOB.

Now Im trying to present the .jpg or .gif file on an jsp page.
Im I suppose to transform it and name it in some way or... ?

Its hard to find information about it here and elsewhere on the net.

would be greatful for any ideas or links!
thanks!

//martin
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the JSP you would use an <img> tag just like for any other image. The src attribute for the tag would reference a servlet rather than an image file.

The servlet would be written to stream the bytes from the DB and set the appropriate response headers.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a getOutputStream() method in the response object but I think you're going to have a hard time working with it from a JSP. JSPs were really meant to return text streams.

I have a servlet that streams images from file. It should be pretty easy to rework it to stream the blob returned from the database.

http://simple.souther.us
At the bottom, look for SimpleStream.

Let me know if you get it to work.
-Ben
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a sample JSP, DealerPicture makes a connection to a (My)SQL DB and retrieves the images stored as BLOBs.

The one trick I learned was to not have any white space in your JSP, as soon as your JSP compiler outputs an out.write("\r\n"); your sunk.



<%@ page contentType="image/jpeg" %><%@ page import="cc.deeley.picture.DealerPicture" %><%@ page import="java.util.*" %><%@ pageimport="java.io.*" %><%@ page import="org.apache.log4j.Logger" %><%! private static Logger logger = Logger.getLogger("cc.deeley.picture"); %><%
int BLOCK_SIZE_WRITE = 512;
String picKey = request.getParameter("picnum");
int picInt =0;
if ((picKey == null) || (picKey.length() == 0)) {
picKey = "Missing picKey parameter";
}
try {
picInt = Integer.parseInt(picKey);
}
catch (NumberFormatException nfe) {
logger.error( " bad picKey: " +picKey);
}

DealerPicture dp = new DealerPicture(picInt);
dp.fetchPic( dp.getPicKey() );

if ( ( dp.getMime().indexOf("jpeg")!= -1 ) || ( dp.getMime().indexOf("jpg")!= -1 ) ){
response.setContentType("image/jpeg");
}
else if ( dp.getMime().indexOf("gif")!= -1 ) {
response.setContentType("image/gif");
}
OutputStream os = response.getOutputStream();
ByteArrayInputStream bs = new ByteArrayInputStream( dp.getPicMass() ) ;
try {
// fs = new FileOutputStream(f);
byte[] buf = new byte[BLOCK_SIZE_WRITE];
int bytes;
while ((bytes = bs.read(buf)) != -1) {
os.write(buf, 0, bytes);
}
}
catch (IOException ex) {
logger.error(ex.toString());
}
finally {
if (os != null) try {os.close();} catch (Exception ex) {}
}
%>
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Ben and I pointed out (Ben more strongly than I), this is not a good thing to be doing from a JSP.

Use a servlet, then you don't have to rely upon tricks to get it to work!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian,

What app server are you running that under?

I know Jasper hard codes a "\n" at the end of the generated service method which is what I thought was causing the IllegalStateException..
 
Brian Deeley
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ben,
Tomcat 4.x , It was the kind of thing that was a quick fix that was slipped into production
Worked good though, six months later we've had no problems with it. A bit slow if you try to load more than 5 jsp pics on a page.
 
martin jenkins
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies!

Im working with jboss as applicationserver, the thing is that Im building the application on springwebframework which handles jsp page-flows and Im suppose to present text and a small .jpg or .gif Image.

If I should use a servlet like some of you suggest this means I have to include the servlet in my .jsp page if that is possible, or just break out of the springwebframework to show the text and the .jpg or .gif recived from the database..

/martin
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian,
Thanks, I tried to do that in a JSP once and gave up.
I can relate to the need jam something in at the last minute.
If you get some free time, you might want to move that to a servlet.
It might not work in future releases of Tomcat or in other servers.

Martin,
Each image in a webpage always involves another trip to the webserver anyway. Try running the example from the link I gave you (it's a war file so you only need to drop it into the webapps directory and run it).

As far as the browser is concerned, the img tags are just pointing to static image files on you server. You should be able to do the same thing to serve up the blobs from your database.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

this means I have to include the servlet in my .jsp page



As I pointed out earlier, you reference the servlet using the <img> tag just like any other image resource.

You can't get around this -- there is no way to mix text and image data using a single response.
[ January 17, 2005: Message edited by: Bear Bibeault ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic