1.0 initialize on the page first
Code
<title> File Upload </title>
Enctype= multi-part form data, and if Enctype= "Multipart/form-data" is more in the properties of the form form, you cannot use the Request.getparameter (Name property)
1.12: Create a test.jsp in the root directory of Web-root, implement the file upload function!
<% @page import= "Java.io.File"%><% @page import= "org.apache.commons.fileupload.disk.DiskFileItemFactory"% ><% @page import= "Org.apache.commons.fileupload.FileItem"%><% @page import= " Org.apache.commons.fileupload.servlet.ServletFileUpload "%><%@ page language=" java "import=" java.util.* " pageencoding= "UTF-8"%><%//Solve garbled request.setcharacterencoding ("Utf-8"); The content of the request passed by the user is displayed, judging the type, if it is a regular normal form, then the execution mode one//otherwise is the file domain, the execution mode two//01. Judge whether it is a normal form or a file upload boolean flag=servletfileupload . ismultipartcontent (Request); if (flag) {//define a parser to parse each item in the request Diskfileitemfactory factory=new diskfileitemfactory (); Parse upload created successfully servletfileupload upload=new servletfileupload (Factory); List<fileitem> filelist=upload.parserequest (Request); Each of the Fileitem represents a TABLE element iterator<fileitem> mytor=filelist.iterator (); while (Mytor.hasnext ()) {Fileitem item=mytor.next (); if (item!=null{//For current validation, normal form or file upload if (Item.isformfield ())//proof is normal form field { Gets the form element content (value values) of String name=item.getfieldname (); The Name property of the form instead of the value if (Name.equals ("username")) {Out.print ( Item.getstring ("Utf-8")); }}else{//is a file upload String filename=item.getname (); Throw to the server directory string path= "/web-inf/folder";//relative path string Paths=this.getservletcont Ext (). Getrealpath (path); Out.print (paths); File File=new file (filename); File Uploadfile=new file (Paths,file.getname ()); Item.write (UploadFile); Out.print ("Upload success"); }}}}%> File Upload code