0

Hi I am reading image from MongoDB and trying to pass that to JSP page but it is not passing properly from my controller. i am thinking i am almost edge to the solution but not getting exactly where i am doing mistake. Please let me know if you find any mistake.

here insertMedia method reading image from file and storing into DB and then returning back that image.

i am passing userMediaSave as image value to JSP, you can get that at tag like

img src=${userMediaSave} alt="Profile images"

My Controller:

@RequestMapping(value = "/userMediaSave", method = RequestMethod.GET)
public ModelAndView mediaLoadSuccess(HttpServletRequest request,HttpServletResponse response,
        @ModelAttribute("mediaBean") MediaBean mediaBean) throws IOException, ServletException {
    ModelAndView model = null;
    File filePart = mediaBean.getMediaImage();
    if (filePart != null) {
        InputStream inputStream = new FileInputStream(filePart);
            GridFSDBFile imageForOutput = null;
            try {
                imageForOutput = loginDelegate.insertMedia(inputStream, request.getContentType(), filePart.getName());
                mediaBean.setExistedMedia(imageForOutput);
                OutputStream out= null;
                if(imageForOutput!=null){
                    InputStream is = imageForOutput.getInputStream();
                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                    int nRead;
                    byte[] data = new byte[16384];
                    while ((nRead = is.read(data, 0, data.length)) != -1) {
                        buffer.write(data, 0, nRead);
                    }
                    byte[]imagenEnBytes = buffer.toByteArray();
                    buffer.flush();
                    response.setContentType("image/jpg" );
                    response.setContentLength(imagenEnBytes.length);
                    model = new ModelAndView("userMedia");
                    request.setAttribute("userMediaSave", imagenEnBytes);
                    return model;
                } else {
                    System.out.println("inside uploadMedia page -ve");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally{
                //out.close();
            }
        }
    return model;
}

JSP is:

<body onload="checkMessage()">
<form:form id="mediaForm" method="get" action="userMediaSave" modelAttribute="mediaBean">
    <table border="1" cellpadding="1" cellspacing="1"
        style="width: 500px;" align="center">
        <tbody>
            <tr>
                <td colspan="4" align="center">Name : Welcome to Media App</td>
            </tr>
            <tr>
                <td rowspan="3">Profile Image</td>
                <td>Upload Images</td>
                <td><input type="file" name="mediaImage" /></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><input name="upload" type="submit" value="Upload" /></td>

            </tr>
        </tbody>
    </table>
    <table border="1" cellpadding="1" cellspacing="1"
        style="width: 500px;" align="center">
        <tbody>
            <tr>
                <td>User has some existed Media Album</td>
            </tr>
            <tr>
                <td> 
                <img src=${userMediaSave} alt="Profile images" style="width:100px;height:100px;">
                </td>
            </tr>

        </tbody>
    </table>
</form:form>

am i passing image from controller to JSP properly? if not please let me know which way i have to pass it.

User12377777
  • 145
  • 2
  • 20
  • You should read up on HTML's `img` element, particularly the purpose of its `src` attribute. You *don't* cram binary data inside it, this attribute is used to point to the URL of the image to be displayed. – kryger Aug 22 '15 at 20:31
  • possible duplicate of [How to retrieve and display images from a database in a JSP page?](http://stackoverflow.com/questions/2340406/how-to-retrieve-and-display-images-from-a-database-in-a-jsp-page) – Neil McGuigan Aug 22 '15 at 20:49
  • Its not duplicate @NeilMcGuigan, let me know how i can pass it. do i have to create another requesting mapping? like @RequestMapping(value = "/image/*". example will be so helpful. – User12377777 Aug 23 '15 at 02:41
  • Hi i got the solution, i created on more controller request with path name, so image src looked into that request to get image. – User12377777 Aug 24 '15 at 07:09

0 Answers0