• Liferay blob data concept in portlet

    Posted on September 30, 2014 by Hamidul Islam in Liferay.

    Liferay Blob Data

    A blob (alternately known as a binary large object, basic large object, BLOB, or BLOb) is a collection of binary data stored as a single entity in a database management system. Blobs are typically images, audio or other multimedia objects, though sometimes binary executable code is stored as a blob.

     Concept covered in this article:

    • Custom plugin portlet with upload capablity
    • Liferay Blob Data Concept
    • Use of Liferay Search Container to display all the uploaded files in row wise
    • Use of serveResource to display individual uploaded file
    • Liferay Blob Data by service.xml

    Liferay Blob Data Concept:

    i) To define blob data in service.xml use type="Blob". For example column name="blobData" type="Blob"

    ii) Actual data uploaded is represented by class com.liferay.portal.kernel.dao.jdbc.OutputBlob

    iii) Use com.liferay.portal.kernel.upload.UploadRequest to handle upload request.

    iv) Use com.liferay.portal.kernel.util.MimeTypesUtil for getting the mime type of the uploaded file.

    v) Use enctype="multipart/form-data" in your form tag in the JSP code.


    Code Details


    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_2_0.dtd">
    <service-builder package-path="com.proliferay.servicebuilder">
        <author>Hamidul Islam</author>
        <entity name="BlobDemo" local-service="true" remote-service="true">
            <column name="blobId" type="long" primary="true" />
            <column name="name" type="String" />
            <column name="mimeType" type="String" />
            <column name="blobData" type="Blob" />
    • name: It stores the name of the uploaded file with file extension.
    • mimeType: Its the mime type of the uploaded file. For example application/pdf, image/jpeg etc.
    • bobData: Actual binary data which is uploaded

    Upload interface code in JSP:

    <portlet:actionURL var='uploadURL' name="upload" />
    <aui:form name="fm" action="<%=uploadURL.toString()%>" method="post" enctype="multipart/form-data">
        <aui:input name="file" type="file" />
        <aui:button type="submit" value="Upload" />

    Display Uploaded Data in row wise:

        emptyResultsMessage="There is no data to display">
                            searchContainer.getStart(), searchContainer.getEnd())%>"
            total="<%= BlobDemoLocalServiceUtil.getBlobDemosCount() %>" />
            <portlet:resourceURL var="viewURL">
                <portlet:param name="dataId"
                    value="<%=String.valueOf(aBlobDemo.getBlobId())%>" />
                value="<%=String.valueOf(row.getPos() + 1)%>" name="Serial No" />
            <liferay-ui:search-container-column-text  href="<%=viewURL.toString() %>" property="name" name="File Name"  />
            <liferay-ui:search-container-column-jsp path="/action.jsp"
                align="right" />
        <liferay-ui:search-iterator />


    process action code for inserting data in database:

    @ProcessAction(name = "upload")
        public void upload(ActionRequest actionRequest,
                ActionResponse actionResponse) throws SystemException,
                FileNotFoundException {
             * PortalUtil.getUploadPortletRequest used for getting upload request
             * From the uploaded file get input stream
            UploadRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);
             * Get the uploaded file name with extension
            String uploadedFileName = uploadRequest.getFileName("file");
            File file = uploadRequest.getFile("file");
            InputStream inputStream = new FileInputStream(file);
             * Below is the actual blob data
            OutputBlob blobData = new OutputBlob(inputStream, file.length());
             * Create and set the primary key
            long id = CounterLocalServiceUtil.increment(BlobDemo.class.getName());
            BlobDemo blobDemo = BlobDemoLocalServiceUtil.createBlobDemo(id);

    process action code to delete record:

         * This process action is for deleting record in the database
        @ProcessAction(name = "delete")
        public void delete(ActionRequest actionRequest,
                ActionResponse actionResponse) throws PortalException,
                SystemException {
            long dataId = ParamUtil.getLong(actionRequest, "dataId");

    serveResource code to view uploaded file:

         * Here serveResource method is used for displaying blob data
        public void serveResource(ResourceRequest resourceRequest,
                ResourceResponse resourceResponse) throws IOException,
                PortletException {
            try {
                long dataId = ParamUtil.getLong(resourceRequest, "dataId");
                BlobDemo blobDemo = BlobDemoLocalServiceUtil.getBlobDemo(dataId);
                if (blobDemo != null) {
                    Blob blob = blobDemo.getBlobData();
                    byte[] binaryData = blob.getBytes(1, (int) blob.length());
                    // resourceResponse.setContentType(blobDemo.getMimeType());
                    resourceResponse.setProperty("Content-disposition","attachement; filename=" + blobDemo.getName());
                    OutputStream o = resourceResponse.getPortletOutputStream();
            } catch (Exception e) {

    blob data in database

    Download Source Code

    Post Tagged with ,

2 Responses so far.

  1. Daulet says:

    Why not display blob data?

  2. Elkk says:

    Have you had success in inserting BLOB in Liferay 6.1 ?

%d bloggers like this: