The SPFLegacyFileUpload sample demonstrates how to use the Commons FileUpload feature in a Struts portlet. The use of the file upload feature is very similar to the implementation in the servlet environment. The Struts file tag is used without change in the JSP and the Struts action uses an ActionForm with a org.apache.struts.upload.FormFile property. The application itself is written using Tiles support in Jakarta Struts.
This sample has several different screens for obtaining the file and subsequently displaying some information about the file. In this sample the configuration has been set to a maximum file size of 200K. The sample displays an error if the file exceeds that size; otherwise the page 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 from 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 maximum file size feature. This information is located in the Struts configuration file.
<controller processorClass="com.ibm.wps.portlets.struts.WpsRequestProcessor"
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 object. Make sure to use the PortletApiUtils to obtain the PortletRequest object from the MultipartRequestWrapper if the PortletRequest object is needed.
PortletApiUtils
portletUtils = PortletApiUtils.getUtilsInstance();
if
(portletUtils != null) {
PortletRequest portletRequest =
(PortletRequest) portletUtils.getPortletRequest(request);
}
The SPFLegacyFileUpload sample demonstrates how to use the Commons FileUpload package in a Struts portlet application. The implementation in the Struts Portlet Framework is very similar to the Jakarta Struts servlet implementation.