Upload files using struts
During file upload, struts uses the default 18 interceptors .
File Upload Action class:
The Action class defines three attributes:
Private File file1; corresponding form:
Private String file1FileName; file name
Private String file1ContentType; file type (MIME)
And provides the set Method for these three attributes.
File Upload form:
UploadAction
Package cn. itcast. upload; import java. io. file; import java. io. IOException; import org. apache. commons. io. fileUtils; import org. apache. struts2.ServletActionContext; import com. opensymphony. xwork2.ActionSupport; public class UploadAction extends ActionSupport {// corresponding form:Private File file1; // File name private String file1FileName; // File type (MIME) private String file1ContentType; public void setFile1 (File file1) {this. file1 = file1;} public void setFile1FileName (String file1FileName) {this. file1FileName = file1FileName;} public void setFile1ContentType (String file1ContentType) {this. file1ContentType = file1ContentType;} public String upload () throws IOException {// get the uploaded folder String path = ServletActionContext. getServletContext (). getRealPath (/upload); // create a File file File = new File (path, file1FileName); // upload the uploaded file to the specified folder FileUtils. copyFile (file1, file); return success ;}}
Struts. xml file Configuration:
/Index. jsp
/Error. jsp