Store and Retrieve Images Using BLOB Data Type in Liferay Plugin Portlet

Objective:

Store and Retrieve images using BLOB data type in Liferay Plugin portlet.

Introduction:

Generally we may get requirement to store and retrieve images from data base.Liferay already have such mechanism to Store and Retrieve images. When we want implement our own data base schema for Plugin portlet to handle these requirement we have to use BLOB data type for service builder.

Environment:

Liferay 6.2 +Tomcat 7.x+MySQL 5.1

Note:

The code will work for portal 6.2 version we can also try for other portal versions too.

Download Image portlet from following location

You can find source and war file


Portlet Screen:


Procedure for deploy portlet:

You can use war file and directly place in your portal deploy folder and test or you can also use source to deploy portlet.

Once portlet is deployed successfully you can see the portlet in sample category name as Blob Service Builder.

Note:

I have hard coded the image id in resource URL please change Id according to your requirement and this is just example I showed image by id.

Storing Images in table:

The following is Article which explains about using BLOB data type in Liferay Plugin portlet.


From above article you will get idea to store images in table.

Retrieve Images

One we store images then we will use serve resource method to display images in JSP pages.
The following is sample code to Display Images in JSP page

Add the following code in JSP page.

<h1>Display Image By Id</h1><br/>
<portlet:resourceURL  var="imageResourceURL">
<portlet:param name="imageId" value="11405"/>
</portlet:resourceURL>
<img src="<%=imageResourceURL.toString()%>" alt="no Image"/>

Add following code in Portlet Action class

@Override
            public void serveResource(ResourceRequest resourceRequest,
                                    ResourceResponse resourceResponse)
                        throws  IOException, PortletException {

                        try {
                                    long imageId=ParamUtil.getLong(resourceRequest,"imageId");
                                    System.out.println("imageId"+imageId);
                                    Photo photo=PhotoLocalServiceUtil.getPhoto(imageId);
                                    if(photo!=null){
                                                Blob image=photo.getData();
                                                byte[ ] imgData = image.getBytes(1,(int)image.length());
                resourceResponse.setContentType("image/jpg");
                OutputStream o = resourceResponse.getPortletOutputStream();
                o.write(imgData);
                o.flush();
                o.close();
                                    }
                                   
                        }
                        catch (Exception e) {
                                    //_log.error(e);
                        }
            }

Author

Meera Prince
Share on Google Plus

About Meera Prince

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment