Hi! I'm posting you a full working example

You can specify the upload folder in the
portlet.xml 1 <init-param>
2 <name>uploadFolder</name>
3 <value>/home/laura/uploads/</value>
4 </init-param>
The File is uploaded to the folder identified by the "uploadFolder" portlet initialization parameter.
view.jsp: 1
2
3<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
4<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
5<%@ page import="javax.portlet.PortletPreferences" %>
6<portlet:defineObjects />
7
8You can upload a new clinical case clicking on "Upload case".
9<br />
10
11<portlet:renderURL var="editCaseURL">
12<portlet:param name="jspPage" value="/edit.jsp" />
13</portlet:renderURL>
14
15<aui:button onClick="<%=editCaseURL%>" value="Upload case" />
edit.jsp: 1<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
2<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%>
3<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%>
4<%@ page import="com.liferay.portal.kernel.util.ParamUtil"%>
5<%@ page import="com.liferay.portal.kernel.util.Validator"%>
6<%@ page import="javax.portlet.PortletPreferences"%>
7
8<portlet:defineObjects />
9<% //... %>
10<liferay-ui:success key="success" message=" YEAH. Case uploaded successfully!" />
11<liferay-ui:error key="error"
12 message="Sorry, an error prevented the upload. Please try again." />
13
14<portlet:actionURL var="editCaseURL" name="uploadCase">
15 <portlet:param name="jspPage" value="/edit.jsp" />
16</portlet:actionURL>
17
18<aui:form action="<%= editCaseURL %>" enctype="multipart/form-data" method="post" >
19<aui:input type="file" name="fileName" size="75"/>
20
21
22<aui:button type="submit" value="Save" />
23</aui:form>
And this is the portlet class :
1package it.laura.test;
2
3import java.io.FileInputStream;
4import java.io.FileNotFoundException;
5import java.io.FileOutputStream;
6import java.io.IOException;
7
8import javax.portlet.ActionRequest;
9import javax.portlet.ActionResponse;
10import javax.portlet.PortletContext;
11
12
13import javax.portlet.PortletException;
14import javax.portlet.PortletPreferences;
15
16
17import com.liferay.portal.kernel.log.LogFactoryUtil;
18import com.liferay.portal.kernel.servlet.SessionErrors;
19import com.liferay.portal.kernel.servlet.SessionMessages;
20import com.liferay.portal.kernel.upload.UploadPortletRequest;
21import com.liferay.portal.kernel.util.FileUtil;
22
23import java.io.File;
24import com.liferay.portal.util.PortalUtil;
25import com.liferay.util.bridges.mvc.MVCPortlet;
26
27
28import org.apache.commons.logging.Log;
29import org.apache.commons.logging.LogFactory;
30
31
32public class UploadCase extends MVCPortlet {
33
34
35 protected String realPath=null;
36 private static Log logger = LogFactory.getLog(UploadCase.class);
37
38 public void uploadCase(ActionRequest actionRequest, ActionResponse actionRresponse) throws PortletException, IOException
39 {
40 String folder=getInitParameter("uploadFolder");
41 realPath = getPortletContext().getRealPath("/");
42 byte[] bytes = null;
43 PortletContext portletContext = actionRequest.getPortletSession().getPortletContext();
44 logger.info("RealPath"+realPath+" UploadFolder :"+folder);
45 try{
46 logger.info(" Into the Try");
47 UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);
48
49 String sourceFileName =uploadRequest.getFileName("fileName");
50
51 File file = uploadRequest.getFile("fileName");
52
53 try {
54 bytes = FileUtil.getBytes(file);
55 } catch (IOException e2) {
56 e2.printStackTrace();
57 }
58 File newFile=null;
59
60 if ((bytes != null) && (bytes.length > 0)) {
61
62 try {
63 newFile = new File(folder+sourceFileName);
64 FileInputStream fileInputStream = new FileInputStream(file);
65 FileOutputStream fileOutputStream = new FileOutputStream(newFile);
66 fileInputStream.read(bytes);
67 fileOutputStream.write(bytes, 0, bytes.length);
68 fileOutputStream.close();
69 fileInputStream.close();
70 SessionMessages.add(actionRequest, "success");
71 }
72 catch (FileNotFoundException e) {
73 System.out.println("File Not Found.");
74 e.printStackTrace();
75 SessionMessages.add(actionRequest, "error");
76 }
77 catch (IOException e1){
78 System.out.println("Error Reading The File.");
79 e1.printStackTrace();
80 SessionMessages.add(actionRequest, "error");
81 }
82 }
83
84} catch (Exception e) {
85 System.out.println("Exception::::"+e.getMessage());
86 SessionMessages.add(actionRequest, "error");
87 }
88}
89
Enjoy it! :-)
Please sign in to flag this as inappropriate.