1 Reply Latest reply on Feb 20, 2009 2:15 PM by piergiorgiolucidi

    Upload portlet

    audran12


      Hello,
      I try to create an upload portlet.
      I have downloaded Jboss portal sources but there is no simple portlet like I need.
      So, if you can help me with a simple java code to upload a file.

      Thank you.

      Audran

        • 1. Re: Upload portlet
          piergiorgiolucidi

          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>