1. Enctype= "Multipart/form-data" attribute must be added to the <form> label
For example: <form action= "Successcase.do?method=modify2" method= "post" enctype= "Multipart/form-data" onsubmit= "return Check () ">
2. You must have the name attribute in the <input> label
For example: <input type= "text" name= "title" Size= "" id= "title"/>
3. Code in Action:
Boolean p=true;//whether to use the default picture
Diskfileitemfactory factory = new Diskfileitemfactory ();
Servletfileupload upload = new Servletfileupload (factory);
list<fileitem> items = upload.parserequest (request);
Iterator<fileitem> it = Items.iterator ();
while (It.hasnext ()) {
Fileitem Tempitem = It.next ();//Get an element from the form
String itemname = Tempitem.getfieldname ();//Get the Name property value of the input label
if (Tempitem.isformfield ()) {//To determine whether this element type is Text,fckediter also returns true
String content = tempitem.getstring ("GBK");
if (Itemname.equals ("title")) {
Sc.settitle (content);
else if (itemname.equals ("content")) {
Sc.setcontent (content);
}
}else {//type is file, uploaded files
if (tempitem.getname () = "") {//If no upload file, use default file
Continue
}
if (! Filedocumentutil.existdocument (Request
. Getrealpath ("/") + "successimages//")) {
Filedocumentutil.createdocument (Request
. Getrealpath ("/") + "successimages//");
}
File tempfile= new file (Request.getrealpath ("/") + "successimages//" +new file (Tempitem.getname ()). GetName ());
Tempitem.write (Tempfile);
Sc.setpicture (Tempfile.getcanonicalpath ());
P=false;
}
}
if (p) {
Sc.setpicture (Request.getrealpath ("/") + "images//logo1.jpg");
}