[jboss-user] [JBoss Portal] - Re: Upload portlet

piergiorgiolucidi do-not-reply at jboss.com
Fri Feb 20 14:15:53 EST 2009


Hi Audran,

here you can see an example to implement an upload portlet using PortletFileUpload class:

http://commons.apache.org/fileupload/apidocs/org/apache/commons/fileupload/portlet/PortletFileUpload.html

You can follow the example below ;)

Hope this helps.

FileUploadPortlet.java:

  | package com.yourcompany.yourapp.view.portlet;
  | 
  | import java.io.FileOutputStream;
  | import java.io.IOException;
  | import java.io.InputStream;
  | import java.util.Iterator;
  | import java.util.List;
  | 
  | import javax.portlet.ActionRequest;
  | import javax.portlet.ActionResponse;
  | import javax.portlet.GenericPortlet;
  | import javax.portlet.PortletException;
  | import javax.portlet.PortletRequestDispatcher;
  | import javax.portlet.RenderRequest;
  | import javax.portlet.RenderResponse;
  | 
  | 
  | import org.apache.commons.fileupload.FileItem;
  | import org.apache.commons.fileupload.FileUploadException;
  | import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  | import org.apache.commons.fileupload.portlet.PortletFileUpload;
  | 
  | public class FileUploadPortlet extends GenericPortlet {
  | 
  | 	protected void doEdit(RenderRequest renderRequest, 
  | 	RenderResponse renderResponse) throws PortletException, IOException {
  | 		renderResponse.setContentType("text/html");		
  | 		PortletRequestDispatcher requestDispacther = getPortletContext()
  | 		.getRequestDispatcher("/yourFileUpload.jsp");
  | 		requestDispacther.include(renderRequest, renderResponse);
  | 	}
  | 
  | 	public void processAction(ActionRequest actionRequest,
  | 	ActionResponse actionResponse) throws PortletException, IOException {
  | 		DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
  | 		PortletFileUpload portletFileUpload = new PortletFileUpload(
  | 				diskFileItemFactory);
  | 		try {
  | 			List fileItemList = portletFileUpload.parseRequest(actionRequest);
  | 			Iterator fileIt = fileItemList.iterator();
  | 			while (fileIt.hasNext()) {
  | 				FileItem fileItem = (FileItem) fileIt.next();
  | 		        InputStream is = fileItem.getInputStream();
  | 		        String filePath = "yourFilePath";
  | 		        FileOutputStream fis = new FileOutputStream(filePath+fileItem.getName());
  | 		        int c;	 
  | 				while((c = is.read()) != -1) {
  | 					fis.write(c);
  | 				}
  | 			}
  | 		} catch (FileUploadException e) {
  | 			e.printStackTrace(System.out);
  | 		}	
  | 	}
  | }
  | 


yourFileUpload.jsp:

  | <%@ taglib uri='http://java.sun.com/portlet' prefix='portlet'%>
  | <portlet:defineObjects/>
  | <form action="<portlet:actionURL/>" method="POST" enctype="multipart/form-data">
  | Upload your file
  | 	<table>
  | 		<tr>
  | 			<td>Your file</td>
  | 			<td><input type="file" name="yourFile"></td>
  | 		</tr>
  | 		<tr>
  | 			<td><input type="submit" name="submit" Value="Submit"></td>
  | 			<td><input type="reset" name="reset" title="Reset"></td>
  | 		</tr>
  | 	</table>	
  | </form>
  | 

portlet.xml:

  | <portlet> 
  |     <description>YourFileUploadForm</description> 
  |     <portlet-name>YourFileUploadForm</portlet-name> 
  |     <portlet-class> com.yourcompany.yourapp.view.portlet.FileUploadPortlet</portlet-class> 
  | 
  |     <supports> 
  |         <mime-type>text/html</mime-type>
  |         <portlet-mode>EDIT</portlet-mode>          
  |     </supports> 
  |  
  |     <portlet-info> 
  |         <title>YourFileUploadForm</title> 
  |         <short-title>YourFileUploadForm</short-title> 
  |     </portlet-info> 
  | </portlet>
  | 
  | 

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4211935#4211935

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4211935



More information about the jboss-user mailing list