3

When developing a Liferay portlet, sometimes you want to use file artifacts. For example, you might want to have a configurable image(s), or the means to let users attach files to your custom service entity.

There are several API's built into Liferay that address this problem. How is each one used?

npskirk
  • 1,188
  • 1
  • 8
  • 21
  • 3
    This question seems like a variation of the question: [How to obtain the download url for a document library content programmatically](http://stackoverflow.com/q/11931592/468763), check-out the [accepted answer](http://stackoverflow.com/a/11932143/468763) to this question to build the URL how liferay builds it. – Prakash K Oct 16 '12 at 04:47
  • I looked at that, but it appears to rely on DLFileEntry Service. This method (DLStoreUtil#addFile) does not store anything in the DLFileEntry table, and thus doesn't expose things like a UUID or file version. I guess you're telling me to store my file some other way (using the DLApp Service or the higher level DLFileEntry Service.) – npskirk Oct 16 '12 at 14:41
  • Have provided a detailed answer, hope that helps. – Prakash K Oct 17 '12 at 05:57

1 Answers1

5

Following are the three methods which I can think of to store and retrieve files.


Method-1

Storage:
Method using the DLStoreUtil as you have shown in your question.

Retrieval:
For this you need to get the file as a stream and then send it to the browser using the following code:

String path = fileName;
// if there is a directory then path would be
// String path = "mydirectory/mySubDirectory/" + fileName; // mydirectory/mySubDirectory/my_File_image_name.png

InputStream inputStream = DLStoreUtil.getFileAsStream(companyId(), CompanyConstants.SYSTEM, path);

long contentLength = DLStoreUtil.getFileSize(companyId(), CompanyConstants.SYSTEM, path);

String contentType = MimeTypesUtil.getContentType(fileName);

ServletResponseUtil.sendFile(request, response, fileName, inputStream, contentLength, contentType);

Liferay uses the above method for downloading attachments for its Message Boards portlet. You can check-out the source code here. I have not tried this but I suppose you can write this code in the serveResource method of your portlet and then provide the resourceURL as the URL to download or use it in the <img> tag.


Method-2: Using DLAppService

If you use this method there would be a database entry in the DLFileEntry table for this file and also the file will appear in the Documents & Media portlets.

Storage:
Example code to add a file:

FileEntry = DLAppServiceUtil.addFileEntry(repositoryId, folderId, sourceFileName, mimeType, title, description, changeLog, bytes, serviceContext);

You can check-out other methods and classes here.

Retrieval:
This is as explained in this answer.


Method-3: Using ImageLocalService (Specifically for Images)

For this method you will need to store the ImageID somewhere for later retrieval of the image.

Storage:
The following is a method to add/update an Image in liferay:

// to Add an image
long imageID = 0;
imageID = CounterLocalServiceUtil.increment();
ImageLocalServiceUtil.updateImage(imageID, bytes);

// to update the same image, pass the existing Image ID
ImageLocalServiceUtil.updateImage(existingImageID, bytes);

Retrieval:

You can write the following code in your JSP:

<img alt="My Image"
     id="myImgID"
     title="This my image stored in liferay"
     src="<%=themeDisplay.getPathImage()%>/any_string_will_do_?img_id=<%=myImageId%>&img_timestamp=<%=someTimestampOrRandomString %>" />

Note: img_timestamp=<%=someTimestampOrRandomString %>", This string is an optional parameter & can be skipped.
This is used so that the browser retrieves the image from the server and not from the browser cache, everytime the page is refreshed.

Hope this helps.

Community
  • 1
  • 1
Prakash K
  • 11,669
  • 6
  • 51
  • 109