Flex _ File Upload processing Java class;

Source: Internet
Author: User

Package com. airflex. util;

Import java. Io. file;
Import java. Io. ioexception;

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;

/**
* @ Author Administrator
*/
/**
* Processes requests for both HTTP <code> Get </code> and <code> post </code>
* Methods.
*
* @ Param request_servlet request
* @ Param response_servlet response
*/
Public class uploadfile extends httpservlet {

Private Static final long serialversionuid = 5425836142860976977l;

// Define the File Upload path
Private string uploadpath = "D: // workspace // FlexNET // flex_src // assets // projectimg //";

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


Public uploadfile (){
Super ();
}


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

/**
* @ Param request
* @ Param response
* @ Throws servletexception
* @ Throws ioexception
*/
Protected void processrequest (httpservletrequest request,
Httpservletresponse response) throws servletexception, ioexception {

System. Out. println ("=> access! ");

Response. setcontenttype ("text/html; charset = UTF-8 ");

// Save the file to the server
Diskfileitemfactory factory = new diskfileitemfactory ();
Factory. setsizethreshold (4096 );

Servletfileupload upload = new servletfileupload (factory );
Upload. setheaderencoding ("UTF-8 ");
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 ));
Response. getwriter (). Write ("uploaded successfully. ");
} Catch (exception e ){
E. printstacktrace ();
Response. getwriter (). Write (E. getmessage ());
}
}
}
} Catch (fileuploadexception e ){
E. printstacktrace ();
Response. getwriter (). Write (E. getmessage ());
System. Out. println (E. getmessage () + "end ");
}
}

Protected void doget (httpservletrequest request,
Httpservletresponse response) throws servletexception, ioexception {
Processrequest (request, response );
}

/**
* Handles the HTTP <code> post </code> method.
*
* @ Param request
* Servlet Request
* @ Param response
* Servlet response
*/
Protected void dopost (httpservletrequest request,
Httpservletresponse response) throws servletexception, ioexception {
Processrequest (request, response );
}

/**
* Returns a short description of the servlet.
*/
Public String getservletinfo (){
Return "Short Description ";
}
}

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.