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?
Following are the three methods which I can think of to store and retrieve files.
Method-1
Storage:
Method using the
DLStoreUtilas 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:
Liferay uses the above method for downloading attachments for its
Message Boardsportlet. You can check-out the source code here. I have not tried this but I suppose you can write this code in theserveResourcemethod of your portlet and then provide theresourceURLas the URL to download or use it in the<img>tag.Method-2: Using
DLAppServiceIf you use this method there would be a database entry in the
DLFileEntrytable for this file and also the file will appear in theDocuments & Mediaportlets.Storage:
Example code to add a file:
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:
Retrieval:
You can write the following code in your JSP:
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.