Uploading files with Liferay

This is a short tutorial for those of you who want to upload a file using Liferay portlets. First you need to give your form in your JSP the correct encoding type:
<form name="<portlet:namespace/>fm" method="post" enctype="multipart/form-data" action="<portlet:actionURL />">

This makes sure that the data you upload is really uploaded. Next you should add the upload button like this :

<input type="file" name="<portlet:namespace/>yourfile" id="<portlet:namespace/>yourfile" />

That´s all in the JSP. If you now add the following in your processAction method, you have access to your uploaded file:

public void processAction(ActionRequest request, ActionResponse response)throws PortletException, IOException {

UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);

String sourceFileName = uploadRequest.getFileName("yourfile");
File file = uploadRequest.getFile("yourfile");

That´s all. Have fun :)

If you like this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.