J2EE中用户上传文件到服务器功能实现

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值