Liferay7 BPM Portal Development 28:portlet file upload, and entity class synchronization update upload

Source: Internet
Author: User
Tags tld

Seize the core--Wang Xin


The core of Liferay file upload is the use of the Uploadportletrequest class

Inheritance relationship
Java.lang.Object
Extended Byjavax.servlet.ServletRequestWrapper
Extended Byjavax.servlet.http.HttpServletRequestWrapper
Extended Bycom.liferay.util.servlet.UploadPortletRequest

To implement a relationship:
Implemented Interfaces:

    • Javax.servlet.http.HttpServletRequest
    • Javax.servlet.ServletRequest

Single File Upload

JSP code

<%@ taglib URI="Http://java.sun.com/portlet_2_0"prefix="Portlet" %><%@ taglib URI="Http://liferay.com/tld/aui"prefix="AUI"%><%@ taglib URI="Http://liferay.com/tld/ui"prefix="Liferay-ui"%><portlet:defineobjects/> <Portlet:actionurlname= "Upload"var= "Uploadfileurl"></Portlet:actionurl> <Aui:formAction= "<%= uploadfileurl%>"enctype= "Multipart/form-data"Method= "POST">     <Aui:inputtype= "File"name= "FileUpload" />        <Aui:buttonname= "Save"value= "Save"type= "Submit" /> </Aui:form>

Java:

import java.io.File; import Javax.portlet.actionrequest;import Javax.portlet.actionresponse;import Org.apache.commons.io.fileutils;import Com.liferay.portal.kernel.upload.uploadportletrequest;import Com.liferay.portal.util.portalutil;import Com.liferay.util.bridges.mvc.MVCPortlet;  Public classRelatedfilesportlet extends Mvcportlet {PrivateFinalStatic intONE_GB =1073741824; PrivateFinalStaticString BaseDir ="/tmp/uploaded/"; PrivateFinalStaticString fileinputname ="FileUpload";  Public voidUpload (actionrequest request, actionresponse response) throws Exception {uploadportletrequest UPL Oadrequest=portalutil.getuploadportletrequest (Request); LongsizeInBytes =uploadrequest.getsize (fileinputname); if(Uploadrequest.getsize (fileinputname) = =0) {            Throw NewException ("the file is empty!"); }         //obtaining a File object from UploadrequestFile UploadedFile =Uploadrequest.getfile (fileinputname); String sourceFileName=Uploadrequest.getfilename (fileinputname); //directory where files are storedFile folder =NewFile (BaseDir); //Make available space checks on the current directory        if(Folder.getusablespace () <ONE_GB) {            Throw NewException ("Insufficient space 1gb!"); }         //the final file pathFile FilePath =NewFile (Folder.getabsolutepath () + File.separator +sourceFileName); //save file to physical pathfileutils.copyfile (UploadedFile, FilePath); //The following are saved in the document libraryThemedisplay Themedisplay =(Themedisplay) Actionrequest.getattribute (Webkeys.theme_display); Servicecontext Servicecontext=servicecontextfactory.getinstance (actionrequest); String ContentType=Mimetypesutil.getcontenttype (UploadedFile); InputStream InputStream=NewFileInputStream (UploadedFile); //the document library path to saveFolder FolderName =Dlapplocalserviceutil.getfolder (Parentrepositoryid, Paren Tfolderid,"Folder Name"); LongFolderID =Foldername.getfolderid (); LongRepositoryid =Foldername.getrepositoryid (); //Save to document libraryFileentry Fileentry =Dlapplocalserviceutil.addfileentry (Themedisplay.getuserid (),                                                                  Repositoryid, FolderID,                                                                  Uploadedfile.getname (), ContentType,"File Name",                                                                  "Description",                                                                  "ChangeLog", InputStream, Uploadedfile.length (), service    Context); }}

Explanation of document library parameter descriptions:
http://blog.csdn.net/lan861698789/article/details/8122203


The demo that saves the file with the entity class
 Public voidaddyourentity (actionrequest request, actionresponse response) throws Exception {Uploadportletrequest Uploadpor Tletrequest=portalutil.getuploadportletrequest (Request); LongArtistID = Paramutil.getlong (Uploadportletrequest,"ArtistID"); String name= Paramutil.getstring (Uploadportletrequest,"name"); intYear = Paramutil.getinteger (Uploadportletrequest," Year"); //more entity-class information    ...... InputStream InputStream= Uploadportletrequest.getfileasstream ("file"); Servicecontext Servicecontext=servicecontextfactory.getinstance (yourentity.class. GetName (), uploadportletrequest); Try{yourentityserviceutil.addyourentity (ArtistID, name, year, InputStream, Servicecontext); Sessionmessages.add (Request,"yourentityadded"); String redirect=paramutil.getstring (Uploadportletrequest,"redirect");    Response.sendredirect (redirect); }    Catch(Exception e) {sessionerrors.add (Request, E.getclass (). GetName ()); if(e instanceof Yourentitynameexception | |e instanceof principalexception) {Response.setrenderparameter ("Jsppage","/html/yourentitys/edit_yourentity.jsp"); }        Else{response.setrenderparameter ("Jsppage","/html/error.jsp"); }    }}


Liferay7 BPM Portal Development 28:portlet file upload, and entity class synchronization update upload

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.