首先导入commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar
PrintWriter out=response.getWriter();
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(1024 * 1024 * 200); //最大为200G
fu.setSizeThreshold(1024 * 1024); //每次上传不能超过1G
String fileurl = WebConst.DBBACKPATH_TEMP;
String fileName =null;
try {
//开始读取上传的信息
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
String filePath = null;
//处理每个表单的字段
while (i.
hasNext()) {
FileItem fi = (FileItem) i.next();
if (fi.isFormField()) {
String content = fi.getString("gb2312");
request.setAttribute(fi.getFieldName(), content);
}
else {
try {
String pathSrc = fi.getName();
int start = pathSrc.lastIndexOf("\\");
fileName = pathSrc.substring(start + 1);
File file = new File(fileurl, fileName);
fi.write(file);
} catch (Exception e) {
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}