Upload files using struts

Source: Internet
Author: User

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
   
  
 



 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.