File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Java Products and Servers and the fly likes liferay struts portlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Java Products and Servers
Bookmark "liferay struts portlet" Watch "liferay struts portlet" New topic
Author

liferay struts portlet

Tapan Thakkar
Greenhorn

Joined: Mar 09, 2010
Posts: 24
hello friends i am creating a liferay struts portlet in liferay 5.2.4. i am creating a application that can download and upload a file.i have created required jsp,action and struts-config.it works when i upload a file but it does not work when i click on download link, i am posting my code here if you find any error then please let me know.
thank you.

1st the view.jsp page will be loaded.

*******struts-config********
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
<form-beans>
<form-bean name="FileUploadAndSave" type="com.sample.form.StrutsUploadAndSaveForm"/>
</form-beans>
<!-- Action Mappings -->
<action-mappings>
<action path="/FileUpload/view" forward="/view.jsp"/>
<action path="/FileUpload/FileUploadAndSave" type="com.sample.action.StrutsUploadAndSaveAction" name="FileUploadAndSave" scope="request" input="/view.jsp">
<forward name="success" path="/downloaduploadedfile.jsp" redirect="true"/>
</action>
</action-mappings>
<!-- Message Resources -->
<message-resources parameter="content.test.Language" />
</struts-config>

**********StrutsUploadAndSaveAction.java*************
package com.sample.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.String;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import com.sample.form.StrutsUploadAndSaveForm;
import java.io.*;

public class StrutsUploadAndSaveAction extends Action
{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
StrutsUploadAndSaveForm myForm = (StrutsUploadAndSaveForm)form;
// Process the FormFile
FormFile myFile = myForm.getTheFile();
String contentType = myFile.getContentType();

//Get the file name
String fileName = myFile.getFileName();
//int fileSize = myFile.getFileSize();
byte[] fileData = myFile.getFileData();
String f = fileData.toString();
//Get the servers upload directory real path name
String filePath = "/opt/Portique/liferay-portal-5.2.4/tomcat-6.0.18/webapps/File_prog-portlet/demo/";
System.out.println("Path ============="+filePath);
/* Save file on the server */
if(!fileName.equals("")){
System.out.println("Server path:" +filePath);
//Create file
File fileToCreate = new File(filePath, fileName);

//If file does not exists create file
if(!fileToCreate.exists()){
FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);
fileOutStream.write(myFile.getFileData());
fileOutStream.flush();
fileOutStream.close();
}
}
//Set file name to the request object
request.setAttribute("fileName",fileName);
return mapping.findForward("success");
}
}

********FileUploadAndSave.jsp**************************

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<%@ taglib uri="http://portals.apache.org/bridges/struts/tags-portlet-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

<html:html locale="true">
<head>
<title>Struts File Upload and Save Example</title>
<html:base/>
</head>
<body bgcolor="white">
<html:form action="/FileUploadAndSave" method="post" enctype="multipart/form-data">
<table>
<tr>
<td align="center" colspan="2">
<font size="4">File Upload on Server</font>
</tr>
<tr>
<td align="left" colspan="2">
<font color="red"><html:errors/></font>
</tr>
<tr>
<td align="right">
File Name
</td>
<td align="left">
<html:file property="theFile"/>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<html:submit>Upload File</html:submit>
</td>
</tr>
</table>
</html:form>
</body>
</html:html>

***************downloaduploadedfile.jsp**************************

<%@ page import="javax.servlet.http.HttpServletRequest" %>
<%@ page import="javax.servlet.http.HttpServletResponse" %>
<%@ page import="java.io.*" %>
<html>
<head>
<title>Success</title>
</head>

<body>
<%
String fileName=null;
try{
System.out.println("Inside try");
fileName=(String)request.getAttribute("fileName");
System.out.println("After getting file name"+fileName);
}
catch(Exception e)
{
System.out.println("Inside Exception");
}
//String filepath = "/opt/demo/";
//String serverpath = "/opt/Portique/liferay-portal-5.2.4/tomcat-6.0.18/webapps/File_prog-portlet/demo/";
//String filepath = serverpath;
File filepath = new File("/opt/Portique/liferay-portal-5.2.4/tomcat-6.0.18/webapps/File_prog-portlet/demo/");
//System.out.println("server path == == == ="+serverpath);
//System.out.println("serverpath===="+serverfilepath);
%>

<p align="center"><font size="5" color="#000080">File Successfully Received</font></p>
<p align="center"><a href="<%=filepath%>">Click here to download</a></p>
</body>
</html>

***********view.jsp******************

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://portals.apache.org/bridges/struts/tags-portlet-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

<portlet:defineObjects />
<html:html locale="true">
<head>
<title>Struts File Upload and Save Example</title>
<html:base/>
</head>
<body bgcolor="white">
<html:form action="/FileUpload/FileUploadAndSave" method="post" enctype="multipart/form-data">
<table>
<tr>
<td align="center" colspan="2">
<font size="4">File Upload on Server</font>
</tr>
<tr>
<td align="left" colspan="2">
<font color="red"><html:errors/></font>
</tr>
<tr>
<td align="right">
File Name
</td>
<td align="left">
<html:file property="theFile"/>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<html:submit>Upload File</html:submit>
</td>
</tr>
</table>
</html:form>
</body>
</html:html>

***************StrutsUploadAndSaveForm.java************************

package com.sample.form;
import org.apache.struts.upload.FormFile;
import org.apache.struts.action.ActionForm;

public class StrutsUploadAndSaveForm extends ActionForm
{
private static final long serialVersionUID = 1L;
private FormFile theFile;

/**
* @return Returns the theFile.
*/
public FormFile getTheFile() {
return theFile;
}
/**
* @param theFile The FormFile to set.
*/
public void setTheFile(FormFile theFile) {
this.theFile = theFile;
}
}

************************************
when i upload a file it successfully upload file to given location but when i click on downloan link it redirect to view.jsp page without downloading a file.

please if you find something wrong then please let me know.

thank you....
Nrupay Shah
Greenhorn

Joined: Dec 27, 2010
Posts: 3
Hi Tapan,

Your code is perfect but only the problem in downloadfile.jsp. You had passed the link of the folder where you are storing the file but not appended the Filename.So how can server will know about the file which you want to download.

Thanks & Regards
Nrupay Shah
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: liferay struts portlet