Implementation of file uploads using Apache FileUpload components

Source: Internet
Author: User
Tags file upload memory usage temporary file storage tomcat

1, you can implement one or more files upload, you can also receive ordinary form form data.

2, a simple test, the memory of the occupancy can be tolerated, and speed can also. Occasionally it can cause memory usage to rise and will not drop, whether it will drop down after a long time has not been tested.

Key points:

1, submitted file upload form of the method property is Post,enctype property for Multipart/form-data.

2, the input label needs to have the name attribute, otherwise can not be taken to the content.

Looking at the implementation of the servlet, the comments are detailed:

Java code

Package ORG.XXM;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.util.Iterator;

Import java.util.List;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;
Import Dareway.org.apache.commons.fileupload.FileItem;
Import dareway.org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
Import Dareway.org.apache.commons.fileupload.disk.DiskFileItemFactory;
Import Dareway.org.apache.commons.fileupload.servlet.ServletFileUpload;

Import Dareway.org.apache.commons.fileupload.util.Streams;  /** * @author xuxiaoming * 2009-03-19 * tomcat6.0 for Server */public class Uploadfileservlet extends HttpServlet {public
	void Doget (HttpServletRequest req, httpservletresponse res) {return;
public void DoPost (HttpServletRequest req, httpservletresponse res) throws IOException {		The enctype in/** * form must be multipart/... * component provides methods to detect the Enctype property of form form * In the Ismultipartcontent method also detects whether post commits * if not
			Post Commit returns false/if (Servletfileupload.ismultipartcontent (req)) {req.setcharacterencoding ("utf-8");
			Diskfileitemfactory factory = new Diskfileitemfactory (); /** * Temporary file storage path to be True/factory.setrepository (new file (".
			/webapps/fileupload/tmp "));
			Maximum memory occupancy factory.setsizethreshold (1024000);
			Servletfileupload SFU = new Servletfileupload (factory);
			Single File maximum byte Sfu.setfilesizemax (102400000);
			The sum of all uploaded files is maximum byte Sfu.setsizemax (204800000);
			List items = null;
			try {items = sfu.parserequest (req);
			catch (Sizelimitexceededexception e) {System.out.println ("size limit exception!");
			catch (Exception e) {e.printstacktrace ();
			} Iterator iter = Items==null?null:items.iterator ();
				while (ITER!= null && iter.hasnext ()) {Fileitem item = (Fileitem) iter.next (); Simple form field if (Item.isformField ()) {System.out.print ("form field:");
					System.out.print (Item.getfieldname () + "");
				System.out.print (Item.getstring ());
					}//File domain else if (!item.isformfield ()) {System.out.println ("Client name:" + item.getname ());
					String fileName = Item.getname (). substring (Item.getname (). LastIndexOf ("\"));
					Bufferedinputstream in = new Bufferedinputstream (Item.getinputstream ()); The file is stored in the upload directory of the project, and this directory must exist bufferedoutputstream out = new Bufferedoutputstream (new FileOutputStream (New Fil E ("..
					/webapps/fileupload/upload/"+ fileName));
				Streams.copy (in, out, true);
		}} else {System.out.println ("enctype error!"); }
	}
}

Because Tomcat is used as a server, the path inside is based on Tomcat and needs to be modified.

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.