Original: http://www.huqiwen.com/2012/10/24/liferay-6-1-development-study-12-file-upload/
Liferay provides a complete file processing, starting from Liferay 6.1 in the processing of files, no longer distinguish between documents and pictures, unified as a file Media library. In the common Portlet plug-in project, if you want to upload files to the Liferay document library, the large stage can be divided into two processes: first, in the portlet will upload files to, the second call Liferay API to upload files to the document library.
(Note: The file upload here only talk about service-side processing, as for the client is the browser using ordinary form file upload, or using Swffileupload or AJAX submission and so on with the server has no relationship, processing methods are the same) one, in the portlet to upload files
There are two ways to get these two, one is the ordinary fileupload processing method, one is using Liferay API 1, the use of FileUpload processing method:
This method is obtained in the same way that the fileupload is used in the normal servlet, the core code is as follows:
Diskfileitemfactory factory = new Diskfileitemfactory ();
Portletfileupload upload = new Portletfileupload (factory);
Servletfileupload upload = new Servletfileupload (factory);
Upload.setsizemax (1024 * 1024 *);
HttpServletRequest ServletRequest = portalutil.gethttpservletrequest (request);
list<fileitem> items = upload.parserequest (ServletRequest);
Note: You can also use this line of code, and if you use this line, the following httpservletrequest ServletRequest = portalutil.gethttpservletrequest (Request) can not, In the upload.parserequest inside pass actionrequest. However, this method can make an error under the WebLogic environment, so if you want to run under WebLogic, use the following servletfileupload.
Other code is just like using fileupload in a normal servlet, where you don't write more. 2, using Liferay API to get uploaded files
It is recommended that you use the following code to upload files.
Uploadportletrequest uploadportletrequest = portalutil.getuploadportletrequest (actionrequest);
String sourceFileName = uploadportletrequest.getfilename ("file");
String ContentType = uploadportletrequest.getcontenttype ("file");
Long size = uploadportletrequest.getsize ("file");
File File = uploadportletrequest.getfile ("file");
InputStream is = uploadportletrequest.getfileasstream ("file");
Note: The file in GetFileName is the name value of <input type= "file" name= "file" >, depending on your adjustment. Second, upload the obtained file to the Liferay document library 1, uploading files
Getting our uploaded files in the portlet is just the first step. Take the uploaded file, how to upload to the Liferay file. The Liferay file upload interface was not found for the time being, so a compromise approach was used for its own research. Use the following interface
Dlapplocalserviceutil.addfileentry (Long userId, long Repositoryid, long folderid,java.lang.string sourceFileName, Java.lang.String mimetype,java.lang.string title, java.lang.String description,java.lang.string changelog, byte[] Bytes,com.liferay.portal.service.servicecontext Servicecontext)
Why is this a compromise approach? Because this type of API at the beginning of the DL is a Liferay internal API, one day it may be changed, but for the application is basically no problem, at most is not elegant.
The parameters for this interface are described individually:
Long userId: User ID for uploading this file
Long Repositoryid: Warehouse Store ID, this ID is generally groupid
Long FolderID: Folder ID, you can create it yourself, or use liferay default such as: Dlfileentrytypeconstants.file_entry_type_id_basic_document, It is best to do their own according to the circumstances of the upload resources, such as the news of the deposit to the news files, blog files, such as blog
String sourcefilename: Source file name of the uploaded file
String mimetype: Is a file type, you can use mimetype = Mimetypesutil.getcontenttype (filename), obtained by file name.
String Title: File title, this is different from the sourceFileName, this title is the final display on the system, can be entered by the user, the source file name is uploaded to obtain the name of the file, can not manually change. can be left blank.
String Description: For a description of this file, you can leave it blank.
String changelog: File modification log, you can leave blank.
Byte[] Bytes: A file's positive file, an array of bytes.
Servicecontext Servicecontext: This class can be used in the following code
Servicecontext Servicecontext = servicecontextfactory.getinstance (DLFileEntry.class.getName (), request);
Get. This object contains some environmental information, such as Groupid,companyid, permissions, portal path, current language, UserID, and so on.
In the practical application can be the above interface to do a layer of encapsulation, as a common file upload interface for other needs file upload place call, the specific package, please do not post my encapsulated code. 2. Get File path
The above file upload after the return of a Fileentry object, if you get to upload the file path, the method below, you can define a method to return the file path, this method for pictures, all kinds of files are valid.
public static String GetFilePath (Fileentry fileentry) {
if (null!=fileentry) {return
"/documents/" + Fileentry.getrepositoryid () + "/" + Fileentry.getfolderid () + "/"
+ Httputil.encodeurl (Htmlutil.unescape ( Fileentry.gettitle ()), true) + "/" + Fileentry.getuuid ();
} else {
//If necessary, here you can define a default picture return
Stringpool.blank;
}
Sometimes if the file is a picture, upload a picture can be very large, how to get a thumbnail. As follows: public static String Getsmallimagepath (Fileentry fileentry) {
String path = GetFilePath (fileentry);
return path+ "imagethumbnail=1";