Struts file upload, get file name and file type

Source: Internet
Author: User

There are also two properties in action: Uploadfilename and Uploadcontenttype, which are used to encapsulate the file name and file type of the uploaded files, respectively. This is unique to the STRUTS2 design: the Strut2 action class directly encapsulates the file contents of the uploaded file via the file Type property, but this file property cannot get the file name and file type of the uploaded files. So Struts2 encapsulates the upload file name and file type information contained in the domain directly into the uploadfilename and Uploadcontenttype properties, which means that the Struts2 for the file field named XXX in the form, Use 3 properties in the corresponding action class to encapsulate the file field information: The XXX property of type file: Used to encapsulate the file content corresponding to the page file field. The Xxxfilename property of type string: The file name used to encapsulate the file corresponding to the file domain. The Xxxcontenttype property of type string: The file type used to encapsulate the file applied by the file domain.   The following is the actual Java code:  jsp page:  <input type= "file" id= "upload" name= "upload"/>   This name is the property corresponding to the action  action class code:     private File upload;    private String uploadfilename;    Private String uploadcontenttype;     public void setupload (File upload) {& nbsp       This.upload = upload;   }        public void Setuploadfilename (St Ring uploadfilename) {        this.uploadfilename = uploadfilename;   }     public void SETUPLOADCOntenttype (String uploadcontenttype) {        This.uploadcontenttype = uploadcontenttype;  & nbsp }  Set 3 properties. Give the set method separately. This way, you will be able to get the uploaded file, file name, file type.   NOTE: this uploadfilename,uploadcontenttype. If the file property name is xxx (private file xxx;). Then this must be Xxxfilename, Xxxcontenttype. Then the set method can be given separately.   Special note that the:,<s:file/> flag is not only tied to upload, but also Uploadcontenttype (the MIME type of the uploaded file) and Uploadfilename (the file name of the uploaded file, The file name does not include the path to the file). Therefore, the <s:file name= "xxx"/> Corresponds to the three properties of XXX, Xxxcontenttype, and xxxfilename inside the action class.

Struts file upload, get file name and file type

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.