Results 1 to 6 of 6

Thread: Display BLOB image with JSTL

  1. #1
    Join Date
    Mar 2010
    Posts
    28

    Default Display BLOB image with JSTL

    Hi everybody,

    I've a little problem: I wrote an application that uploads an image in a MySQL blob. It worksd fine. But I cannot retreive and display it ... I show you what I've done :

    Controller:
    Code:
        @RequestMapping("show")
        public ModelAndView show(HttpServletRequest req) {
    	if (logger.isDebugEnabled()) {
    	    logger.debug("Display image with id " + req.getParameter("id"));
    	}
    	ModelAndView mav = null;
    	if (req.getParameter("id") == null || req.getParameter("id").length() == 0) {
    	    return null;
    	} else {
    	    try {
    		Integer id = Integer.parseInt(req.getParameter("id"));
    		WebPicture wp = webPictureDao.get(id);
    		mav = new ModelAndView("showPicture");
    		if (logger.isDebugEnabled()) {
    		    logger.debug("serving data : " + wp.getData());
    		}
    		OutputStream os = new ByteArrayOutputStream();
    		// wp.getData() = byte[]
    		os.write(wp.getData());
    		mav.addObject("data", os);
    	    } catch (NumberFormatException ex) {
    		return null;
    	    } catch (Exception e) {
    		logger.warn("Unable to fetch picture with id " + req.getParameter("id"), e);
    	    }
    	}
    	return mav;
        }
    Then, to display it I simply wrote this:
    <%@ page language="java" contentType="image/jpeg" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <c:out value="${data}" />
    But when I visit the page to display the image, that just displays the url I entered (I think this is because firefox write a complete html page with an img tag in the body, and the url as alt attribute)

    Any idea of what's wrong?

    thanks!

  2. #2
    Join Date
    Mar 2010
    Posts
    28

    Default

    I solved the problem.

    Thanks for the help

  3. #3
    Join Date
    Oct 2009
    Location
    Chennai,India
    Posts
    90

    Default

    what was the fix;

    I saw in spring sample application they were using img tag

    <img src="imageContent?name=<%= image.getName() %>
    Rgds
    Vedamoorthi Vairam

  4. #4
    Join Date
    Oct 2008
    Posts
    2

    Default How?

    I am having similar problem, I want to load a blob impage file in jsp. There will be many blob files which will be shown in one jsp. How did this display problem was solved?
    Thanks for the help
    Adi

  5. #5
    Join Date
    Oct 2005
    Location
    Mobile, AL
    Posts
    345

    Default

    Typically, you use the <img> tag and specify the src="<path to a controller>". The "path to a controller" streams back the bytes of the image.

  6. #6
    Join Date
    Oct 2008
    Posts
    2

    Default

    Thanks MartyJones. Your suggestion was helpful. I was able to reach similar conclusion after some google searching and thinking. But i could not decide weather my solution was right or not. Your anser made me comfortable to use this solution. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •