servlet
HashMap map=new HashMap();
boolean isMultipart= ServletFileUpload.isMultipartContent(req);
if (isMultipart){
FileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);
try {
List<FileItem> items= upload.parseRequest(req);
Iterator<FileItem> iter= items.iterator();
while (iter.hasNext()){
FileItem item= iter.next();
if (!item.isFormField()){
String fieldName = item.getFieldName();
String fileName= item.getName();
map.put(fieldName,fileName);
String upload1 = req.getServletContext().getRealPath("upload");
String[] outs = upload1.split("out");
String homePath=outs[0]+"web\\img\\upload";
File file1=new File(homePath,fileName);
item.write(file1);
}else {
String fieldName = item.getFieldName();
String val = item.getString("utf-8");
map.put(fieldName,val);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
Html
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="fileImg">
<input type="text" name="username">
</form>