文件上传 commons-fileupload.jar

jsp:

<%@ page language="java" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="./FileUpload" method="post" enctype="multpart/form-data" name="form1">
<input type="file" name="file">
<input type="submit" name="Submit" value="upload">
</form>
<hr>
<form action="./FileUpload" method="post" enctype="multpart/form-data" name="uploadform">
<table border="1" width="450" cellpadding="4" cellspacing="2" bordercolor="#9BD7FF">
<tr>
	<td width="100%" colspan="2">
		<input type="file" name="x" size="40">	
	</td>
</tr>
<tr>
	<td width="100%" colspan="2">
		<input type="file" name="y" size="40">	
	</td>
</tr>
<tr>
	<td width="100%" colspan="2">
		<input type="file" name="z" size="40">	
	</td>
</tr>
</table>
<br/><br/>
<table>
<tr>
	<td align="center">
		<input type="submit" name="upload" value="开始上传">
	</td>
</tr>
</table>
</form>
</body>
</html>


servlet :

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		System.out.println(request.getContentLength());
		System.out.println(request.getContentType());
		DiskFileItemFactory factory = new DiskFileItemFactory();
		factory.setSizeThreshold(4096);//maximum size that will be stored in memory
		factory.setRepository(new File(uploadPath));//the location for saving data that is larger than getSizeThreshold()
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setSizeMax(3595016);
		try{
			List fileItems = upload.parseRequest(request);
			Iterator iter = fileItems.iterator();
			String regExp = ".+\\\\(.+)$";//正则表达式  过滤路径得到文件名
			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 || "".equals(name) || 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");
							}
						}
					}
					//保存上传的文件到指定目录
					item.write(new File(uploadPath+"/"+name));
					out.print(name+"  "+size+"<br>");
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			out.print(e.getMessage());
		}
	}

需要jar文件:

commons-fileupload.jar,  commons-io.jar 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值