STRUTS2 Tutorial 7: Upload any number of files

Source: Internet
Author: User
Tags file upload

First, upload a single file

Uploading files is a feature that many web programs have. A component for uploading files has been provided in struts1.x. In Struts2, a more easily manipulated upload file component is provided. The difference is that the struts1.x upload component needs a actionform to pass the file, and the Struts2 upload component is an interceptor (this interceptor is not configured and automatically loaded). In this article first introduced how to upload a single file with Struts2, the last introduction to use STRUTS2 upload any number of files.

The ability to upload a single file with Struts2 is very easy to implement, as long as the normal action can be used. But in order to get some information about uploading files, such as uploading file names, uploading file types and uploading stream objects, you need to add some getter and setter methods to the action class according to certain rules.

In Struts2, the method used to get and set the object (Struts2 upload the file to a temporary path and use to open the temporary file) is getupload and setupload. The way to get and set the file name is Getuploadfilename and Setuploadfilename, and the way to get and set the content type of uploading files is Getuploadcontenttype and Setuploadcontenttype. Here is the complete code for the action class for uploading:

 package action; Import*; import com.opensymphony.xwork2.ActionSupport; public class Uploadaction extends A
    Ctionsupport {private File upload;
    Private String Uploadfilename;
    Private String Uploadcontenttype;
    Public String Getuploadfilename () {return uploadfilename;
    public void Setuploadfilename (String fileName) {this.uploadfilename = filename;
    Public File Getupload () {return upload;
    The public void Setupload (File upload) {this.upload = upload;
    public void Setuploadcontenttype (String contentType) {this.uploadcontenttype=contenttype;
    Public String Getuploadcontenttype () {return this.uploadcontenttype; Public String Execute () throws Exception { is = new (Uploa
        D); OS = new ("d:\\upload\\" + uploadfilename); BytE buffer[] = new byte[8192];
        int count = 0;
        while ((count = (buffer)) > 0) {os.write (buffer, 0, count);
        } os.close ();
        Is.close ();
    return SUCCESS; }

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: 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.