Wednesday, January 8, 2014

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


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


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.


Liferay 6.2 +Tomcat 7.x+MySQL 5.1


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.


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"/>
<img src="<%=imageResourceURL.toString()%>" alt="no Image"/>

Add following code in Portlet Action class

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

                        try {
                                    long imageId=ParamUtil.getLong(resourceRequest,"imageId");
                                    Photo photo=PhotoLocalServiceUtil.getPhoto(imageId);
                                                Blob image=photo.getData();
                                                byte[ ] imgData = image.getBytes(1,(int)image.length());
                OutputStream o = resourceResponse.getPortletOutputStream();
                        catch (Exception e) {


Meera Prince


Post a Comment

Recent Posts

Recent Posts Widget

Popular Posts