Java Upload file

Source: Internet
Author: User

Uploading files

1. How to upload files

2. Implementing the Jar package code with FileUpload

1String TempPath = This. Getservletcontext (). Getrealpath ("temp");2Diskfileitemfactory factory=NewDiskfileitemfactory (1024*1024,NewFile (TempPath));3         4Servletfileupload upload=Newservletfileupload (factory);5Upload.setheaderencoding ("Utf-8");6         Try {7@SuppressWarnings ("Unchecked")8list<fileitem> items =upload.parserequest (request);9             if(items!=NULL){Ten                  for(Fileitem fileitem:items) { One                     if(Fileitem.isformfield ()) { AString FieldName =fileitem.getfieldname (); -String value = fileitem.getstring ("Utf-8"); -System.out.println (fieldname+ "----->" +value); the}Else{ -String FileName =fileitem.getname (); -InputStream in =Fileitem.getinputstream (); -StringBuilder filepath=NewStringBuilder ( This. Getservletcontext (). Getrealpath ("Upload")); +Filepath.append ("/"). Append (NewDate (). GetTime ()). Append (Filename.substring (Filename.lastindexof (".").))); -OutputStream out=NewFileOutputStream (filepath.tostring ()); +                         byte[] b=New byte[1024]; A                         intLen=0; at                          while((Len=in.read (b))!=-1){ -Out.write (b, 0, Len); -                         } - out.close (); - in.close (); - Fileitem.delete (); in                     } -                 } to             } +}Catch(fileuploadexception e) { -             //TODO auto-generated Catch block the e.printstacktrace (); *}

Java Upload file

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.