问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/12903778/how-do-i-use-the-different-file-storage-and-retrieval-apis-in-liferay