// 執行文件上傳的方法和用戶添加的方法: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";
}