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