public class Uoloadservlet extends HttpServlet {
Private static final long serialversionuid = 1L;
protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
1. Create a file disk factory
Diskfileitemfactory diskfileitemfactory = new Diskfileitemfactory ();
2. Create a file upload object using the file disk factory
FileUpload FileUpload = new FileUpload (diskfileitemfactory);
3. Use the parse method of the file upload object to parse the request data stream
try {
list<fileitem> lists = fileupload.parserequest (request);
for (Fileitem fileitem:lists) {
String fieldName = Fileitem.getfieldname ();
if (Fileitem.isformfield ()) {
This entry is a normal input box
GetFieldName represents the name of the parameter
Return parameter values
String value = fileitem.getstring ("Utf-8");
SYSTEM.OUT.PRINTLN ("Parameter name:" +fieldname+ "-Parameter value:" +value);
}else{
File Upload Type
Get file name
String name = Fileitem.getname ();//
File input stream
InputStream InputStream = Fileitem.getinputstream ();
FileOutputStream FileOutputStream = new FileOutputStream ("c:\\users\\huyoufu\\desktop\\upload\\" +name);
Using Ioutils
Ioutils.copy (InputStream, FileOutputStream);
Fileoutputstream.close ();
Inputstream.close ();
}
}
} catch (Fileuploadexception e) {
E.printstacktrace ();
}
4. Return to list set with parsed Fileitem
Function: File Upload