代码来自网络,稍作改动...
引入的jar包
commons-io-1.2.jar
commons-fileupload-1.1.1.jar
Servlet类
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
*
* @author Administrator
*
*/
public class FileUploadServlet extends HttpServlet {
private String uploadPath = "D:\\addnetFile\\"; // 用于存放上传文件的目录
private File tempPath = new File("D:\\addnetFile\\tmp\\"); // 用于存放临时文件的目录
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html; charset=GB2312");
PrintWriter out = res.getWriter();
System.out.println(req.getContentLength());
System.out.println(req.getContentType());
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
factory.setRepository(new File("d:\\File\\addnetFile\\temp"));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(1000000);
try {
List fileItems = upload.parseRequest(req);
Iterator iter = fileItems.iterator();
// 正则匹配,过滤路径取文件名
String regExp = ".+\\\\(.+){1}quot;;
// 过滤掉的文件类型
String[] errorType = { ".exe", ".com", ".cgi", ".asp" };
Pattern p = Pattern.compile(regExp);
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
// 忽略其他不是文件域的所有表单信息
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if ((name == null || name.equals("")) && size == 0)
continue;
Matcher m = p.matcher(name);
boolean result = m.find();
if (result) {
for (int temp = 0; temp < errorType.length; temp++) {
if (m.group(1).endsWith(errorType[temp])) {
throw new IOException(name + ": wrong type");
}
}
try {
// 保存上传的文件到指定的目录
item.write(new File("d:\\" + m.group(1)));
out.print(name + " " + size + "<br>");
} catch (Exception e) {
out.println(e);
}
} else {
throw new IOException("fail to upload");
}
}
}
} catch (IOException e) {
out.println(e);
} catch (FileUploadException e) {
out.println(e);
}
}
public void init() throws ServletException {
}
}
Web前端Html页
<html>
<head>
<title>物料维护</title>
<link rel="stylesheet" href="../style/drp.css">
<script src="../script/client_validate.js"></script>
<script language="javascript">
</script>
</head>
<body class="body1">
<form name="itemForm" target="_self" id="itemForm" method="post" enctype="multipart/form-data" action="../servlet/item/FileUploadServlet">
<div align="center">
<table width="95%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td>
</td>
</tr>
</table>
<table width="95%" border="0" cellspacing="0" cellpadding="0"
height="8">
<tr>
<td width="522" class="p1" height="2" nowrap>
<img src="../images/mark_arrow_03.gif" width="14" height="14">
<b>基础数据管理>>物料维护>>上传物料图片</b>
</td>
</tr>
</table>
<hr width="97%" align="center" size=0>
<table width="95%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="29">
<div align="right">
物料代码:
</div>
</td>
<td>
</td>
</tr>
<tr>
<td height="26">
<div align="right">
物料名称:
</div>
</td>
<td>
</td>
</tr>
<tr>
<td height="26">
<div align="right">
物料规格:
</div>
</td>
<td>
</td>
</tr>
<tr>
<td height="26">
<div align="right">
物料型号:
</div>
</td>
<td>
</td>
</tr>
<tr>
<td height="26">
<div align="right">
类别:
</div>
</td>
<td>
</td>
</tr>
<tr>
<td height="26">
<div align="right">
计量单位:
</div>
</td>
<td>
</td>
</tr>
<tr>
<td height="74">
<div align="right">
图片:
</div>
</td>
<td>
<img src="../images/about.gif" width="85" height="49">
</td>
</tr>
<tr>
<td width="22%" height="29">
<div align="right">
<font color="#FF0000">*</font>选择图片:
</div>
</td>
<td width="78%">
<input name="fileName" type="file" class="text1" size="40" maxlength="40">
</td>
</tr>
</table>
<hr width="97%" align="center" size=0>
<div align="center">
<input name="btn_upload" class="button1" type="submit"
id="btn_upload" value="上传">
<input name="btnBack" class="button1" type="button" id="btnBack"
value="返回" onClick="history.go(-1)">
</div>
</div>
</form>
</body>
</html>
web.xml配置自己应该可以完成的吧,也可以选择向导,很简单的...