异步文件上传的操作

导入form 架包

<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<script type="text/javascript" src="jquery/jquery-1.12.4.js"></script>
	<script type="text/javascript" src="jquery/jquery.form.js"></script>
	<script type="text/javascript">
		$(function(){
			$("#btn").click(function(){
				var option = { 
					url:"AddFileServlet",
					type:"post",
					dataType:"json",
					success:function(result){
						alert(result.retmsg);
						if(result.retcode==1){
							location.href="SelectServlet";
						}
					}
				}
				$("#myform").ajaxSubmit(option);
					});
			});
	</script>

  </head>
  
  <body>
  		<h2>填写需要添加的数据</h2>
   		<form method="post" action="AddFileServlet" enctype="multipart/form-data" id="myform">
   			<table border="1px">
   				<tr>
   					<td><input type="text" name="studentno"/></td>
   					<td>学生编号</td>
   				</tr>
   				<tr>
   					<td><input type="text" name="loginpwd"/></td>
   					<td>登录密码</td>
   				</tr>
   				<tr>
   					<td><input type="text" name="studentname"/></td>
   					<td>学生姓名</td>
   				</tr>
   				<tr>
   					<td><input type="text" name="sex"/></td>
   					<td>性别</td>
   				</tr>
   				<tr>
   					<td><input type="text" name="gradeID"/></td>
   					<td>年级号</td>
   				</tr>
   				<tr>
   					<td><input type="text" name="phone"/></td>
   					<td>手机号码</td>
   				</tr>
   				<tr>
   					<td><input type="text" name="address"/></td>
   					<td>地址</td>
   				</tr>
   				<tr>
   					<td><input type="date" name="borndate"/></td>
   					<td>出生日期</td>
   				</tr>
   				<tr>
   					<td><input type="text" name="email"/></td>
   					<td>邮箱</td>
   				</tr>
   				<tr>
   					<td><input type="text" name="identifyId"/></td>
   					<td>身份证号</td>
   				</tr>
   				<tr>
   					<td><input type="file" name="photo" id ="photo"/></td>
   					<td>上传照片</td>
   				</tr>
   				<tr>
   					<td colspan="2"><input type="button" id="btn" value="添加"></td>
   				</tr>
   			</table>
   		</form>
  </body>
</html>
public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Student stu = new Student();
		PrintWriter out = response.getWriter();
		//判断是否为上传类型
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		//获取要上传的路径
		String uploadpath = request.getSession().getServletContext().getRealPath("images");
		if(isMultipart){
			//1.创建文件工厂对象
			FileItemFactory factory = new DiskFileItemFactory();
			//2.创建文件上传对象
			ServletFileUpload upload = new ServletFileUpload(factory);
			//3.解析Form表单中所有文件
			List<FileItem> items = null;
			try {
				items = upload.parseRequest(request);
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//4.通过迭代器遍历集合
			Iterator<FileItem> iter = items.iterator();
			while (iter.hasNext()) {
				FileItem item = iter.next();
				if (item.isFormField()) {
					if (item.getFieldName().equals("studentno")) {
						stu.setStudentNo(Integer.parseInt(item.getString()));
					}
					if (item.getFieldName().equals("loginpwd")) {
						stu.setLoginPWd(item.getString("UTF-8"));
					}
					if (item.getFieldName().equals("studentname")) {
						stu.setStudentName(item.getString("UTF-8"));
					}
					if (item.getFieldName().equals("sex")) {
						stu.setSex(Integer.parseInt(item.getString()));
					}
					if (item.getFieldName().equals("gradeID")) {
						stu.setGradeId(Integer.parseInt(item.getString()));
					}

					if (item.getFieldName().equals("phone")) {
						stu.setPhone(item.getString("UTF-8"));
					}
					if (item.getFieldName().equals("address")) {
						stu.setAddress(item.getString("UTF-8"));
					}
					if (item.getFieldName().equals("borndate")) {
						stu.setBornDate(item.getString("UTF-8"));
					}
					if (item.getFieldName().equals("email")) {
						stu.setEmail(item.getString("UTF-8"));
					}
					if (item.getFieldName().equals("identifyId")) {
						stu.setIdentityCard(item.getString("UTF-8"));
					}
				} else {
					String filename = item.getName();
					stu.setPhoto(filename);
					File myfile = new File(uploadpath,filename);
					try {
						item.write(myfile);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			StudentServiceImpl ssi = new StudentServiceImpl();
			int result = ssi.add(stu);
			Map<String,Object> maps = new HashMap<String,Object>();
			if(result==1){
				maps.put("retcode",1);
	    		maps.put("retmsg","添加成功");
			}else{
				maps.put("retcode",-1);
	    		maps.put("retmsg","添加失败");
			}
			String str = JSON.toJSONString(maps);
			out.print(str);
		}
	}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值