FileUpload series: (2) File Upload example

Source: Internet
Author: User


Example

package com.rk.web.servlet;import java.io.bufferedreader;import java.io.file;import  java.io.fileinputstream;import java.io.ioexception;import java.io.inputstream;import  Java.io.inputstreamreader;import java.io.unsupportedencodingexception;import java.util.list;import  java.util.UUID;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.fileitemfactory;import  org.apache.commons.fileupload.FileUploadException;import  org.apache.commons.fileupload.disk.diskfileitemfactory;import  Org.apache.commons.fileupload.servlet.servletfileupload;import org.apache.commons.io.fileutils;public  class uploadservlet extends httpservlet {// upload directory, save uploaded resources Public void  doget (HttpSErvletrequest request, httpservletresponse response) throws servletexception,  ioexception {/********* File Upload component:  process file upload ************/try {// 1.  file upload factory fileitemfactory  factory = new diskfileitemfactory ();// 2.  Create File upload core tool class servletfileupload  Upload = new servletfileupload (Factory);//  One, sets the maximum size allowed for a single file:  30mupload.setfilesizemax (30*1024*1024);//  Second, set the total size of the file upload form:  80mupload.setsizemax (80*1024*1024);//  third,   set the Upload form file name encoding/ /  equivalent: request.setcharacterencoding ("UTF-8"), Upload.setheaderencoding ("UTF-8");// 3.  judgment:  Whether the current form is a file upload form if  (upload.ismultipartcontent (Request)) {// 4.  Convert the request data to a Fileitem object, and then use the collection package list< Fileitem> list = upload.parserequest (Request);//  traverse:  to get each uploaded data for  (FileItem  item: list) {//  judgment: Plain Text data if  (Item.isformfield ()) {//  Plain Text data string fieldname  = item.getfieldname ();  FORM element name String content = item.getstring ();//  the form element name,  corresponding data//item.getstring (" UTF-8 ");   Specify the encoding System.out.println (fieldname + " = " + content);}   Upload file (file stream)  ---->  upload data to else {//  file in upload directory string fieldname =  Item.getfieldname ();//  form element name String name = item.getname ();//  file name String content  = item.getstring ();//  form element name,  corresponding data string type = item.getcontenttype ();//   File Type Inputstream in = item.getinputstream (); //  upload file stream/* *   four, File name duplicate  *   for different users of the same name file, do not want to overwrite!  *   background Processing:  adds a unique tag to the user! */// a.  randomly generates a unique tag string id =  Uuid.randomuuid (). toString ();// b.  and filename stitching name = id + "#" + name;//  Gets the upload base path String path = getservletcontext (). Getrealpath ("/upload");//  Create destination file File file  = new&nbSp File (path,name);//  tool class, document upload Item.write (files); Item.delete ();    // Delete system-generated temporary files System.out.println ();}}} Else {system.out.println ("The current form is not a file upload form, processing failed!") ");}}  catch  (exception e)  {e.printstacktrace ();}}   Manual Implementation process private void upload (httpservletrequest request)  throws ioexception, Unsupportedencodingexception {/*request.getparameter ("");  // get/postrequest.getquerystring ();  //  get the data for the Get Commit  request.getinputstream (); //  get the data for the post submission    *//********* * * Manually get file Upload form data ************///1.  get form data stream Inputstream in =  request.getinputstream ();// 2.  Conversion Stream Inputstreamreader instream = new inputstreamreader (in,  "UTF-8");//3.  Buffered Stream Bufferedreader reader = new bufferedreader (instream);//  output Data string str =  null;while  ((Str = reader.readline ())  != null)  {system.out.println (str);}   Close Reader.close (); Instream.close (); In.close ();} Public void dopost (Httpservletrequest request, httpservletresponse response) throws  servletexception, ioexception {this.doget (Request, response);}}


FileUpload series: (2) File Upload example

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.