Struts2 file upload, Set temp directory and limit file size and bulk upload

Source: Internet
Author: User
Tags file upload

Under src New, struts.properties file, filename is fixed, used to change some of the default configuration.

You can find the default configuration file under Struts2 jar package, and generally do not modify that file.

Struts.multipart.savedir=d:/upload
struts.multipart.maxsize=9000000


Struts2 uploading files is not as simple as the direct upload we imagined.

You need to upload it to the temporary file first, and then move to the specified directory.


The bulk upload is the case, and now the form is set to the same name.

	<form action= "Upload.action" method= "post" enctype= "Multipart/form-data" >
    	
    	<input type= "file" name= Files "><br>
    	<input type=" file "name=" files "><br>
    	<input type=" file "name=" Files " ><br>
    	
    	<input type= "Submit" value= "Upload" >
    </form>

And then handle it in action.


private list<file> files; Name to be consistent.
	private list<string> filesfilename;//The latter two naming formats are fixed
	private list<string> filescontenttype;

All that's left is to traverse it and conquer it.

	public string Execute () throws Exception {
                //placed under the files file	
		string path = Servletactioncontext.getrequest (). Getrealpath ("/files");
		int n = files.size ();
		for (int i=0; i<n; i++) {
			InputStream is = new FileInputStream (Files.get (i));
			OutputStream OS = new FileOutputStream (New File (Path,filesfilename.get (i)));
			byte[] buffer = new BYTE[1024*10];
			int length = 0;
			
			while (-1!= (length = is.read (buffer)) {
				os.write (buffer, 0, length);
			}
			Is.close ();
			Os.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: 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.