The file upload sample demonstrates how to use the commons-fileupload feature in a Struts portlet. The use of the file upload features are very similar to the implementation for the servlet enviroment. The Struts file tag is used without change in the JSP, and the Struts Action uses an ActionForm that has a property of the org.apache.struts.upload.FormFile. The application is also written using the Tiles support in Jakarta Struts.
The sample has several different screens for getting the file and displaying some information about the file. The sample has been configured to also set a maximum file size of 200K. The sample will display an error if the file exceeds that size; otherwise the page just displays the file name and size.
The file is obtained by using the support built into Jakarta Struts. The JSP uses the Struts file tag to request that the browser allow the user to select a file from the local file system.
<html:form action="/displayFile" method="POST" enctype="multipart/form-data">
<table border="0"
width="100%">
<tr>
<th align="right">
<bean:message key="example.prompt.file.name"/>:
</th>
<td
align="left">
<html:file property="formFile"/>
</td>
</tr>
<tr>
<td>
<html:submit
value="Submit"/>
</td>
</tr>
</table>
</html:form>
The com.ibm.portal.struts.example.fileupload.actions.DisplayFileAction class will use the FormFile object to retrieve information about the file. Information about the org.apache.struts.upload.FormFile can be obtained through the Jakarta Struts documentation.
FileUploadForm
fileUploadForm = (FileUploadForm)
form;
// get the form file
FormFile
formFile = fileUploadForm.getFormFile();
if (formFile != null )
{
log.debug("file
name is " + formFile.getFileName() +
" and file size is " + formFile.getFileSize()
);
// get some
basic information from the file
fileUploadForm.setFileName( formFile.getFileName() );
fileUploadForm.setFileSize( formFile.getFileSize() );
forward = mapping.findForward("success");
formFile.destroy();
}
The file upload sample also demonstrates the max file size exceeded feature. This is configured in the struts configuration file.
<controller
processorClass="com.ibm.portal.struts.portlet.WpRequestProcessor"
maxFileSize="200K">
<!--
The "input" parameter on "action" elements is the name of a
local or global "forward" rather than
a module-relative path -->
<set-property
property="inputForward"
value="true"/>
</controller>
Note: The request object that is passed in the org.apache.struts.action.Action execute method is a MultipartRequestWrapper. Make sure to use the PortletApiUtils to obtain the PortletRequest object from the MultipartRequestWrapper if the portlet request object is needed.
PortletApiUtils
portletUtils = PortletApiUtils.getUtilsInstance();
if
(portletUtils != null) {
PortletRequest portletRequest =
(PortletRequest) portletUtils.getPortletRequest(request);
}
The FileUpload sample demonstrates how to implement a Struts application in portal that uses the commons file upload package. The implementation in the Struts Portlet Framework is very similar to the Jakarta Struts servlet implementation.