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
service.xml:
<?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> <namespace>PROLIFERAY</namespace> <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" /> </entity> </service-builder>
- 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" /> </aui:form>
Display Uploaded Data in row wise:
<liferay-ui:search-container emptyResultsMessage="There is no data to display"> <liferay-ui:search-container-results results="<%=BlobDemoLocalServiceUtil.getBlobDemos( searchContainer.getStart(), searchContainer.getEnd())%>" total="<%= BlobDemoLocalServiceUtil.getBlobDemosCount() %>" /> <liferay-ui:search-container-row className="com.proliferay.servicebuilder.model.BlobDemo" modelVar="aBlobDemo"> <portlet:resourceURL var="viewURL"> <portlet:param name="dataId" value="<%=String.valueOf(aBlobDemo.getBlobId())%>" /> </portlet:resourceURL> <liferay-ui:search-container-column-text 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-container-row> <liferay-ui:search-iterator /> </liferay-ui:search-container>
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); blobDemo.setName(uploadedFileName); blobDemo.setBlobData(blobData); blobDemo.setMimeType(MimeTypesUtil.getContentType(file)); BlobDemoLocalServiceUtil.addBlobDemo(blobDemo); }
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"); BlobDemoLocalServiceUtil.deleteBlobDemo(dataId); }
serveResource code to view uploaded file:
/** * Here serveResource method is used for displaying blob data */ @Override 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.setContentType("application/application-download"); resourceResponse.setProperty("Content-disposition","attachement; filename=" + blobDemo.getName()); OutputStream o = resourceResponse.getPortletOutputStream(); o.write(binaryData); o.flush(); o.close(); resourceResponse.flushBuffer(); } } catch (Exception e) { } }