Use commons to upload files

Source: Internet
Author: User
This code was written before the project was created. for fear of future use, paste it and save it.
Load commons-fileupload-1.2.jar before using

/*
* @ (#) Usersmanagerbean
*
* Copyright 2008 School of software, Yunnan University.
* All Rights Reserved
*/
Package user;

Import java. Io. file;
Import java. Io. ioexception;
Import java. Io. printwriter;
Import java. util. iterator;
Import java. util. List;

Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;

Import org. Apache. commons. fileupload. fileitem;
Import org. Apache. commons. fileupload. fileuploadexception;
Import org. Apache. commons. fileupload. disk. diskfileitemfactory;
Import org. Apache. commons. fileupload. servlet. servletfileupload;

/**
* File Upload
* @ Version 1.0.0.0 2008-5-18
* @ Author meteorwj
*/
Public class fileuploadservlet extends httpservlet {

/**
* Processes requests for both HTTP <code> Get </code> and <code> post </code> methods.
* @ Param request Servlet Request
* @ Param response servlet response
*/
// Define the File Upload path
Private string uploadpath = "E: // upload //";

// Restrict the file upload size
Private int maxpostsize = 100*1024*1024;

Public fileuploadservlet (){
Super ();
}

Public void destroy (){
Super. Destroy ();
}

Protected void processrequest (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
System. Out. println ("access! ");
Response. setcontenttype ("text/html; charset = UTF-8 ");
Printwriter out = response. getwriter ();

// Save the file to the server
Diskfileitemfactory factory = new diskfileitemfactory ();
Factory. setsizethreshold (4096 );
Servletfileupload upload = new servletfileupload (factory );
Upload. setsizemax (maxpostsize );
Try {
List fileitems = upload. parserequest (request );
Iterator iter = fileitems. iterator ();
While (ITER. hasnext ()){
Fileitem item = (fileitem) ITER. Next ();
If (! Item. isformfield ()){
String name = item. getname ();
System. Out. println (name );
Try {
Item. Write (new file (uploadpath + name ));
} Catch (exception e ){
E. printstacktrace ();
}
}
}
} Catch (fileuploadexception e ){
E. printstacktrace ();
System. Out. println (E. getmessage () + "end ");
}
}

}

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.