1. Guide Package: commons-fileupload-1.2.2 & commons-io=2.0.1
2. Set the uploaded form properties: <form action= "Dealuploadservlet" method= "post" enctype= "Multipart/form-data" > </form>
3. Processing in Dealuploadservlet:
3.1 fileitemfactory fac=new diskitemfactory ();
Servletfileupload su=new Servletfileupload (FAC);
List<fileitem> list=su.parserequest (Request);
3.2 Loop list, respectively, for non-file and file domain processing
3.2.1 Non-file: if (Item.isformfield ()) {
stored in different variables depending on the non-file content
if (item.getfieldname () = = "txt")//Get name for TXT item
{
String txt=item.getstring ("Utf-8");
}
}
3.2.2 File fields:
else {
Limit the types of uploads
if (Item.getname (). SUBSTRING (
Item.getname (). LastIndexOf (".") + 1) = = "Docx") {
Get the path to the server side
String path = This.getservletcontext (). Getrealpath (
"Upload");
File F = new file (path);
if (!f.exists ()) {
Create a storage path
F.mkdirs ();
}
try {
Write in File
Item.write (New File (Path, New String (Item.getname ()
. GetBytes (), "Utf-8")));//Prevent file name garbled problem
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}else{
PrintWriter Pw=response.getwriter ();
Pw.write ("file format is not docx");
}
}
Java_ file domain upload