Hello
I want to upload and store a file in local tomcat server file server (something like CKEditor).
So I've created a portlet.
These are my important files:
MyUploadFilePortlet.java:
1package com.mine.portlets;
2
3import com.liferay.portal.kernel.log.Log;
4import com.liferay.portal.kernel.log.LogFactoryUtil;
5import com.liferay.portal.kernel.upload.UploadPortletRequest;
6import com.liferay.portal.util.PortalUtil;
7
8import javax.portlet.ActionRequest;
9import javax.portlet.ActionResponse;
10import javax.portlet.GenericPortlet;
11import javax.portlet.PortletException;
12import javax.portlet.PortletPreferences;
13import javax.portlet.PortletRequestDispatcher;
14import javax.portlet.RenderRequest;
15import javax.portlet.RenderResponse;
16
17import java.io.File;
18import java.io.IOException;
19
20/**
21 * Portlet implementation class MyUploadFilePortlet
22 */
23public class MyUploadFilePortlet extends GenericPortlet {
24
25 protected String editJSP;
26 protected String viewJSP;
27
28 private static Log _log = LogFactoryUtil.getLog(MyUploadFilePortlet.class);
29
30 public void init() {
31 editJSP = getInitParameter("edit-jsp");
32 viewJSP = getInitParameter("view-jsp");
33 }
34
35 public void processAction(
36 ActionRequest actionRequest, ActionResponse actionResponse)
37 throws IOException, PortletException {
38
39 String description = actionRequest.getParameter("description");
40
41 UploadPortletRequest lUploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);
42
43 if (lUploadPortletRequest.getFileName("file") != null && !"".equals(lUploadPortletRequest.getFileName("file"))) {
44 File file = lUploadPortletRequest.getFile("file");
45 String fileName = lUploadPortletRequest.getFileName("file");
46 file.setLastModified(System.currentTimeMillis());
47 String fileAbsPath = file.getAbsolutePath();
48
49 PortletPreferences prefs = actionRequest.getPreferences();
50
51 prefs.setValue("VIDEODESCRIPTION", description);
52 prefs.setValue("VIDEOURL", fileAbsPath);
53 prefs.store();
54
55 System.out.println("VIDEODESCRIPTION=" + description + " path=" + fileAbsPath + " file name=" + fileName);
56 }
57 }
58
59 public void doEdit(
60 RenderRequest renderRequest, RenderResponse renderResponse)
61 throws IOException, PortletException {
62
63 include(editJSP, renderRequest, renderResponse);
64 }
65
66 public void doView(
67 RenderRequest renderRequest, RenderResponse renderResponse)
68 throws IOException, PortletException {
69
70 PortletPreferences prefs = renderRequest.getPreferences();
71
72 String defaultValue = "";
73
74 String description = (String) prefs.getValue("VIDEODESCRIPTION", defaultValue);
75 String fileAbsPath = (String) prefs.getValue("VIDEOURL", defaultValue);
76
77 renderRequest.setAttribute("ATTRIBUTE_VIDEODESCRIPTION", description);
78 renderRequest.setAttribute("ATTRIBUTE_VIDEOURL", fileAbsPath);
79
80 include(viewJSP, renderRequest, renderResponse);
81 }
82
83 protected void include(
84 String path, RenderRequest renderRequest,
85 RenderResponse renderResponse)
86 throws IOException, PortletException {
87
88 PortletRequestDispatcher portletRequestDispatcher = getPortletContext().getRequestDispatcher(path);
89
90 if (portletRequestDispatcher == null) {
91 _log.error(path + " is not a valid include");
92 }
93 else {
94 portletRequestDispatcher.include(renderRequest, renderResponse);
95 }
96 }
97}
view.jsp
1<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
2
3<portlet:defineObjects />
4
5<%
6String description = (String)request.getAttribute("ATTRIBUTE_VIDEODESCRIPTION");
7String fileAbsPath = (String)request.getAttribute("ATTRIBUTE_VIDEOURL");
8%>
9
10<h3>View mode</h3>
11<p>video description: <%= description %></p>
12<p>video path: <%= fileAbsPath %></p>
edit.jsp
1<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
2
3<portlet:defineObjects />
4
5<h3>Edit mode</h3>
6<form action="<portlet:actionURL />" method="POST" name="<portlet:namespace />fm" enctype="multipart/form-data">
7 <input type="text" name="<portlet:namespace />description" /><br />
8 <input type="file" name="<portlet:namespace />file"><br />
9 <input type="submit" />
10</form>
So what I want to do exactly is play a video file in view mode.
But my first step is to store uploaded file in local server file system. The form for doing that is en edit mode. So far, in view mode I can get absolute path of the file, file name etc...
How to store an uploaded file ?
I've search through the forum but answers found did not work for me. I've tried DiskFileItemFactory class but I'm missing a lot of things.
Please help.
Please sign in to flag this as inappropriate.