public class FileUploadServlet extends AbstractItemServlet {
private File uploadPath;
private File tempPath;
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//form提交采用multipart/form-data,无法采用req.getParameter()取得数据
//String itemNo = req.getParameter("itemNo");
//System.out.println("itemNo======" + itemNo);
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(4096);
// the location for saving data that is larger than getSizeThreshold()
factory.setRepository(tempPath);
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum size before a FileUploadException will be thrown
upload.setSizeMax(1000000 * 20);
try {
List fileItems = upload.parseRequest(req);
String itemNo = "";
for (Iterator iter = fileItems.iterator(); iter.hasNext();) {
FileItem item = (FileItem) iter.next();
//是普通的表单输入域
if(item.isFormField()) {
if ("itemNo".equals(item.getFieldName())) {
itemNo = item.getString();
}
}
//是否为input="type"输入域
if (!item.isFormField()) {
String fileName = item.getName();
long size = item.getSize();
if ((fileName == null || fileName.equals("")) && size == 0) {
continue;
}
//截取字符串 如:C:\WINDOWS\Debug\PASSWD.LOG
fileName = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length());
item.write(new File(uploadPath, fileName));
itemManager.uploadItemImage(itemNo, fileName);
}
}
res.sendRedirect(req.getContextPath() + "/servlet/item/SearchItemServlet");
} catch (Exception e) {
e.printStackTrace();
throw new ApplicationException("上传失败!");
}
}
public void init() throws ServletException {
uploadPath = new File(getServletContext().getRealPath("upload"));
System.out.println("uploadPath=====" + uploadPath);
//如果目录不存在
if (!uploadPath.exists()) {
//创建目录
uploadPath.mkdir();
}
//临时目录
tempPath = new File(getServletContext().getRealPath("temp"));
if (!tempPath.exists()) {
tempPath.mkdir();
}
//显示调用父类的init方法
super.init();
}
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="./servlet/item/FileUploadServlet" method="post"
enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<input type="submit" name="Submit" value="upload">
</form>
<form action="./servlet/HelloWord" method="post">
<input type="submit" />
</form>
<form name="uploadform" method="POST" action="./servlet/item/FileUploadServlet"
ENCTYPE="multipart/form-data">
<table border="1" width="450" cellpadding="4" cellspacing="2"
bordercolor="#9BD7FF">
<tr>
<td width="100%" colspan="2">
文件1:
<input name="x" size="40" type="file">
</td>
</tr>
<tr>
<td width="100%" colspan="2">
文件2:
<input name="y" size="40" type="file">
</td>
</tr>
<tr>
<td width="100%" colspan="2">
文件3:
<input name="z" size="40" type="file">
</td>
</tr>
</table>
<br />
<br />
<table>
<tr>
<td align="center">
<input name="upload" type="submit" value="开始上传" />
</td>
</tr>
</table>
</form>
</body>
</html>
http://www.oschina.net/question/941545_88473