|
Many applications gives the utility to upload the data from client applications to the server. Struts also gives the flexibility to upload the files using the org.apache.struts.upload.FormFile class. The org.apache.struts.upload.FormFile class is a special class in struts which represent the property type of file. The following example will demonstrate to upload any txt file and place it in the upload folder by the name of temp.txt.
Example:-
(1) Struts-config.xml file
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <!-- ================================================ Form Bean Definitions --> <form-beans> <form-bean name="inputForm" type="com.visualbuilder.InputForm" />
</form-beans> <!-- ========================================= Global Exception Definitions --> <global-exceptions></global-exceptions> <!-- =========================================== Global Forward Definitions --> <global-forwards></global-forwards> <!-- =========================================== Action Mapping Definitions --> <action-mappings> <action path="/submit" type="com.visualbuilder.InputAction" name="inputForm" scope="request" validate="false" input="/index.jsp"/> </action-mappings>
<controller maxFileSize="2M" /> <!-- ======================================== Message Resources Definitions --> <message-resources parameter="MessageResources" />
</struts-config>
(2) InputForm.java File
package com.visualbuilder;
import org.apache.struts.upload.FormFile; import org.apache.struts.validator.ValidatorForm;
public class InputForm extends ValidatorForm {
private FormFile fileName;
public FormFile getFileName() { |