spring--mvc添加用户及用户头像上传

          spring--mvc添加用户及用户头像上传  

 添加用户步骤:

    1.用ajax获取省份信息

    2.添加用户

  代码:register.jsp

 1 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 2 <title>Insert title here</title>
 3 <script type="text/javascript" src="${path }/JS/jquery-3.2.1.js"></script>
 4 <script type="text/javascript">
 5     $(function() {
 6         $("#uname").blur(function() {
 7             $.post("${path}/users/isVaildUname.action", {
 8                 uname : $(this).val()
 9             }, function(data) {
10                 if (data > 0) {
11                     $("#div").html("用户名已存在!");
12                     $(this).select();
13                 } else {
14                     $("#div").html("ok");
15                 }
16             });
17         });
18     });
19 </script>
20 </head>
21 <body>
22     <form action="${path }/upload.action" method="post"
23         enctype="multipart/form-data">
24         用户名:<input type="text" name="uname" id="uname">
25         <div id="div"></div>
26         密码:<input type="password" name="upass"> 
27         出生日期: <input value="${users.birthday }" name="birthday" /> <br> <input
28             type="hidden" id="hiddens" value="${path }"><br> 
29         省份: <select name="cid" id="convinceBean"></select><br>
30         选择图片: <input type="file" name="p" accept="images/jpg,images/jpeg,images/png">
31         <br>
32         <input type="submit" value="Register">
33     </form>
34 </body>
35 <script type="text/javascript" src="${path }/JS/jquery-3.2.1.js"></script>
36 <script type="text/javascript" src="${path }/JS/getConvince.js"></script>
37 </html>

  2.controller代码:

 1 package com.controller;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.util.UUID;
 6 
 7 import javax.annotation.Resource;
 8 
 9 import org.springframework.stereotype.Controller;
10 import org.springframework.web.bind.annotation.RequestMapping;
11 import org.springframework.web.multipart.MultipartFile;
12 
13 import com.bean.UsersBean;
14 import com.service.IUsersService;
15 
16 @Controller
17 public class UplocdFileController {
18 
19     @Resource(name = "usersService")
20     private IUsersService usersService;
21 
22     @RequestMapping("/upload")
23     /* public String uploadFile(MultipartFile headpic, UsersBean user) { */
24     public String uploadFile(MultipartFile p, UsersBean user) {
25         if (p != null) {
26             // 指定文件上传后的物理位置
27             String path = "D:\大数据\month6\program\";
28             // 获取文件名
29             String filename = p.getOriginalFilename();
30             // 改名字
31             String newfilename = UUID.randomUUID() + filename.substring(filename.lastIndexOf("."));
32 
33             File f = new File(path, newfilename);
34             try {
35                 if (!f.exists()) {
36                     // 上传文件
37                     p.transferTo(f);
38                     user.setPicture(newfilename);
39                 }
40             } catch (IllegalStateException e) {
41                 // TODO Auto-generated catch block
42                 e.printStackTrace();
43             } catch (IOException e) {
44                 // TODO Auto-generated catch block
45                 e.printStackTrace();
46             }
47         }
48         usersService.addUser(user);
49         return "redirect:index.jsp";
50     }
51 }

注意: 

    jsp页面的name属性要与bean类(数据库字段)属性名相同,但上传图片的标签的name属性不能与bean类(数据库字段)属性一样,得与controller得传入参数一样!!

    使用uuid防止上传图片名字重复!

原文地址:https://www.cnblogs.com/meiLinYa/p/8880484.html