JBoss Community Archive (Read Only)

GateIn Portal 3.8

Upload Component

In this section, you will learn how to configure the Upload service that is defined by the org.exoplatform.upload.UploadService class.

This can be configured with the following XML code:

   <component>
     <type>org.exoplatform.upload.UploadService</type>
     <init-params>
       <value-param>
         <name>upload.limit.size</name>
         <description>Maximum size of the file to upload in MB</description>
         <value>10</value>
       </value-param>
     </init-params>
   </component>

This code allows uploading files with the default size limit (10MB). The default value unit is in Megabytes.

This limitation will be used by default by all applications if no application-specific limit is set. Setting a different limit for applications is discussed in a later section.

If the value is set to 0, the upload size is unlimited.

By default, GateIn allows everyone to upload resources to server through the Upload component on UI or by accessing directly the "/upload" handler. Therefore, we may cause some problems about the server disk space. If you do not want everyone who was not logged be able upload to your server, you should configure UploadHandler as follows:

<external-component-plugins>
    <target-component>org.exoplatform.web.WebAppController</target-component>
    <component-plugin>
      <name>UploadHandler</name>
      <set-method>register</set-method>
      <type>org.exoplatform.web.handler.UploadHandler</type>
      <init-params>
        <value-param>
          <name>public-access-restriction</name>
          <value>true</value>
        </value-param>
      </init-params>
		</component>
<extenrnal-component-plugins>       

Use the upload component

  1. Create an org.exoplatform.webui.form.input.UIUploadInput object type by using one of three following constructors:

    • The default constructor that allows uploading the file with the size of 10 MB.

      public UIUploadInput(String name, String bindingExpression, int limitFile)
    • This constructor allows you to customize the size limit of uploaded files by using the limitSize parameter. The default value unit is in Megabytes.

      public UIUploadInput(String name, String bindingExpression,int limitFile, int limitSize)
    • This constructor allows you to customize the size limit and the value unit by using the limitSize and unit parameters respectively.
      In GateIn, you can set the value unit to Megabytes (MB), Kilobytes (KB) or Gigabytes (GB).

      public UIUploadInput(String name, String bindingExpression, int limitFile, int limitSize, UploadUnit unit)

      The following is an example using the third form:

      PortletRequestContext pcontext = (PortletRequestContext)WebuiRequestContext.getCurrentInstance();
      PortletPreferences portletPref = pcontext.getRequest().getPreferences();
      int limitFile = Integer.parseInt(portletPref.getValue("uploadFileLimit", "1").trim());
      int limitSize = Integer.parseInt(portletPref.getValue("uploadFileSizeLimit", "").trim());
      UploadUnit limitUnit = UploadUnit.valueOf(portletPref.getValue("uploadFileLimitUnit", "MB").trim());
      UIUploadInput uiInput = new UIUploadInput("upload", "upload", limitFile, limitSize, limitUnit);
  2. Obtain the limit from the XML configuration by adding the following code to either portlet.xml or portlet-preferences.xml:

    <! The number of files are uploaded -->
    	<preference>
    		<name>uploadFileLimit</name>
    		<value>3</value>
    		<read-only>false</read-only>
    	</preference>
    <! The size limit -->
    	<preference>
    		<name>uploadFileSizeLimit</name>
    		<value>300</value>
    		<read-only>false</read-only>
    	</preference>
    <! The unit limit -->
    	<preference>
    		<name>uploadFileLimitUnit</name>
    		<value>KB</value>
    		<read-only>false</read-only>
    	</preference>

    The 0 value means the upload size is unlimited, and the value unit is set to MegaBytes.

  3. Use the getUploadDataAsStream() method to get the uploaded data:

    UIUploadInput input = (UIUploadInput)uiForm.getUIInput("upload");
    InputStream[] inputStreams = input.getUploadDataAsStreams();
    ...

    The upload service stores a temporary file on the file system during the upload process. When the upload is finished, the service must be cleaned to:

    • Delete the temporary file.

    • Delete the classes used for the upload.

  4. Use the removeUploadResource(String uploadId) method defined in the upload service to purge the file:

    UploadService uploadService = uiForm.getApplicationComponent(UploadService.class);
    UIUploadInput uiChild = uiForm.getChild(UIFormUploadInput.class);
    for(String uploadId : uiChild.getUploadIds()) {
        uploadService.removeUpload(uploadId);
    }
    Saving the uploaded file

    Ensure the file is saved before the service is cleaned.

JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-10 13:20:14 UTC, last content change 2013-11-25 09:25:26 UTC.