We already know how to upload file in document and media portlet. Today we will see how to upload a file in local machine . I created a folder on my desktop with name textdata. When we upload a file it will save in this folder . Here i also check that file must be a text file if you upload a pdf or jpeg it will give error.
So lets start this Step by Step:-
Step 1:-Create a project and portlet
Create a project and then create a portlet in it .Here my project name is upload-download-portlet and portlet name is Demo.
Step 2:-Change the jsp
Change your view.jsp as:-
view.jsp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %> | |
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> | |
<portlet:defineObjects /> | |
<portlet:actionURL var="uplaodURL" name="uploadFile"></portlet:actionURL> | |
<portlet:resourceURL var="downloadURL"></portlet:resourceURL> | |
<liferay-ui:success key="success" message="File Upload SuccessFully"/> | |
<liferay-ui:error key="failure" message="Upload Error" /> | |
<liferay-ui:error key="file.extension.error" message="Upload only text File" /> | |
<b>Please Upload a Text Document</b> | |
<br><br> | |
<form action="<%=uplaodURL%>" method="post" enctype="multipart/form-data"> | |
<input type="file" name="textFile"/><br><br> | |
<input type="Submit" name="Submit"/> | |
</form> | |
<a href="<%= downloadURL %>">Click Here for Download</a> |
Explanation:-
Here we create two urls :-
1)uploadURL:- Action URL for upload
2)downloadURL:- Resource URL(Ajax) for Download
Here we also use success and failure messages for Validation.
Step 3:-Change the Controller(Java)
Change the Portlet file as:-
Demo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.demo; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.nio.file.Files; | |
import javax.portlet.ActionRequest; | |
import javax.portlet.ActionResponse; | |
import javax.portlet.PortletException; | |
import javax.portlet.ResourceRequest; | |
import javax.portlet.ResourceResponse; | |
import org.apache.commons.io.IOUtils; | |
import com.liferay.portal.kernel.portlet.LiferayPortletConfig; | |
import com.liferay.portal.kernel.servlet.SessionErrors; | |
import com.liferay.portal.kernel.servlet.SessionMessages; | |
import com.liferay.portal.kernel.upload.UploadPortletRequest; | |
import com.liferay.portal.kernel.util.Validator; | |
import com.liferay.portal.util.PortalUtil; | |
import com.liferay.util.bridges.mvc.MVCPortlet; | |
public class Demo extends MVCPortlet { | |
public final void uploadFile(final ActionRequest request, final ActionResponse response) { | |
SessionMessages.add(request, ((LiferayPortletConfig) getPortletConfig()).getPortletId() + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE); | |
final String uploadDirectoryPath = "C:\\Users\\Aditya.bhardwaj\\Desktop\\textdata"; | |
final UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(request); | |
final File file = uploadPortletRequest.getFile("textFile"); | |
//String fileName = uploadPortletRequest.getFileName("textFile"); | |
OutputStream out = null; | |
InputStream in = null; | |
try { | |
final String fileType = Files.probeContentType(file.toPath()); | |
if (Validator.isNotNull(fileType)) { | |
if (fileType.equalsIgnoreCase("text/plain")) { | |
in = new FileInputStream(file); | |
out = new FileOutputStream(new File(uploadDirectoryPath, "hello.txt")); | |
IOUtils.copy(in, out); | |
SessionMessages.add(request, "success"); | |
} else { | |
SessionErrors.add(request, "file.extension.error"); | |
} | |
} else { | |
SessionErrors.add(request, "failure"); | |
} | |
} catch (final IOException e) { | |
SessionErrors.add(request, "failure"); | |
} finally { | |
try { | |
if (Validator.isNotNull(out)) { | |
out.flush(); | |
out.close(); | |
} | |
if (Validator.isNotNull(in)) { | |
in.close(); | |
} | |
} catch (final IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
@Override | |
public final void serveResource(final ResourceRequest request, final ResourceResponse response) throws IOException, PortletException { | |
final String downloadDirectoryPath = "C:\\Users\\Aditya.bhardwaj\\Desktop\\textdata"; | |
final File outputFile = new File(downloadDirectoryPath, "hello.txt"); | |
response.setContentType("text/plain"); | |
response.addProperty("Content-disposition", "atachment; filename=hello.txt"); | |
OutputStream out = null; | |
InputStream in = null; | |
try { | |
out = response.getPortletOutputStream(); | |
in = new FileInputStream(outputFile); | |
IOUtils.copy(in, out); | |
} catch (final IOException e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
if (Validator.isNotNull(out)) { | |
out.flush(); | |
out.close(); | |
} | |
if (Validator.isNotNull(in)) { | |
in.close(); | |
} | |
} catch (final IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Explanation:-
Here i created a folder textData on Desktop where i upload the file. File are save with name hello.txt.
Step 4:-Check the output
You can download the source code from here.
Related Post:-
Basic web content in Liferay
Categorization in web content
Structure and Template in Web Content
Fetch Web Content Programmatically
Get Journal Article Using SAX Parser
Form Handling in Spring Portlet
Embedding a Web Content in a Portlet
Fetch Latest Version Journal Article or Web Content in Liferay
No comments:
Post a Comment