// 执行文件上传的方法和用户添加的方法:userAdd
@RequestMapping(value = "/userAdd")
public String userAdd(//
User user, //
@RequestParam("pic") MultipartFile file, HttpSession session) {
// 获得文件参数:名称,路径
String fileName = Commons.getFileName(file.getOriginalFilename());// temp.png
String target = "E:/java53fileUpload" + File.separator + fileName;
// 上传文件
try {
file.transferTo(new File(target));
} catch (Exception e) {
e.printStackTrace();
}
// 设置绑定的user对象属性值
String path = "/fileUpload" + File.separator + fileName;
User u = (User) session.getAttribute("USER_LOGIN");
user.setPicpath(path);
user.setCreationDate(new Date());
user.setCreatedBy(u.getId());
// 执行用户的添加
Long key = userService.addUser(user);
System.out.println("上传" + user + "成功!");
return "redirect:/user/userList";
}