From 1dcafacb8d03d2373b9de6fe15c7fd99f5f2c7b4 Mon Sep 17 00:00:00 2001 From: Veal98 <1912420914@qq.com> Date: Tue, 19 Jan 2021 17:33:32 +0800 Subject: [PATCH] Complete function about update avator and password --- docs/70-账号设置.md | 233 +++++ log/community/log_error.log | 458 +++++++++ log/community/log_info.log | 893 ++++++++++++++++++ log/community/log_warn.log | 401 ++++++++ .../community/controller/HomeController.java | 3 + .../community/controller/LoginController.java | 2 +- .../community/controller/UserController.java | 164 ++++ .../com/greate/community/dao/UserMapper.java | 2 +- .../greate/community/service/UserService.java | 24 +- src/main/resources/application.properties | 8 +- .../static/js/bs-custom-file-input.js | 167 ++++ src/main/resources/templates/index.html | 4 +- .../resources/templates/site/setting.html | 96 +- upload/3a5bfa9ec32d411c921329db4860225c.jpeg | Bin 0 -> 40457 bytes upload/442e4088a94c474892296dcc4c1b8671.jpg | Bin 0 -> 189398 bytes upload/57d9b9d4e33c4edb89f115b1d0dc87be.png | Bin 0 -> 8044 bytes upload/69000079d8bd4ef8a93ed6b4856bb9e0.png | Bin 0 -> 3541 bytes upload/c1fddcbf8cd6410ba8cfb4cd944499fb.png | Bin 0 -> 8044 bytes upload/c7def7ad01194035a7e8435bbd1ebe3a.jpeg | Bin 0 -> 20980 bytes upload/deeeab311d79438bb4e7c0937a4d16f7.png | Bin 0 -> 8044 bytes 20 files changed, 2377 insertions(+), 78 deletions(-) create mode 100644 docs/70-账号设置.md create mode 100644 src/main/java/com/greate/community/controller/UserController.java create mode 100644 src/main/resources/static/js/bs-custom-file-input.js create mode 100644 upload/3a5bfa9ec32d411c921329db4860225c.jpeg create mode 100644 upload/442e4088a94c474892296dcc4c1b8671.jpg create mode 100644 upload/57d9b9d4e33c4edb89f115b1d0dc87be.png create mode 100644 upload/69000079d8bd4ef8a93ed6b4856bb9e0.png create mode 100644 upload/c1fddcbf8cd6410ba8cfb4cd944499fb.png create mode 100644 upload/c7def7ad01194035a7e8435bbd1ebe3a.jpeg create mode 100644 upload/deeeab311d79438bb4e7c0937a4d16f7.png diff --git a/docs/70-账号设置.md b/docs/70-账号设置.md new file mode 100644 index 00000000..08669236 --- /dev/null +++ b/docs/70-账号设置.md @@ -0,0 +1,233 @@ +# 账号设置 + +--- + +## 修改头像(上传文件) + +- 请求:必须是 Post 请求 +- 表单:enctype = "multipart/form-data" +- Spring MVC:通过 MultipartFile 处理上传文件 + +### DAO + +```java +/** + * 修改头像 + * @param id + * @param headerUrl + * @return + */ +int updateHeader(int id, String headerUrl); +``` + + + +### Service + +```java +/** + * 修改用户头像 + * @param userId + * @param headUrl + * @return + */ +public int updateHeader(int userId, String headUrl) { + return userMapper.updateHeader(userId, headUrl); +} +``` + +### Controller + +```java +@Controller +@RequestMapping("/user") +public class UserController { + + ...... + + /** + * 修改用户头像 + * @param headerImage + * @param model + * @return + */ + @PostMapping("/upload") + public String uploadHeader(MultipartFile headerImage, Model model) { + if (headerImage == null) { + model.addAttribute("error", "您还没有选择图片"); + return "/site/setting"; + } + + // 获取文件后缀名 + String fileName = headerImage.getOriginalFilename(); + String suffix = fileName.substring(fileName.lastIndexOf(".")); // 文件后缀名 + if (StringUtils.isBlank(suffix)) { + model.addAttribute("error", "图片文件格式不正确"); + return "/site/setting"; + } + + // 对用户上传的图片文件进行重新随机命名 + fileName = CommunityUtil.generateUUID() + suffix; + // 确定文件存放的路径 + File dest = new File(uploadPath + "/" + fileName); + try { + // 存储图片文件 + headerImage.transferTo(dest); + } catch (IOException e) { + logger.error("上传文件失败" + e.getMessage()); + throw new RuntimeException("上传文件失败,服务器发生异常", e); + } + + // 更新当前用户的头像的路径(web 访问路径) + // http://localhost:8080/echo/user/header/xxx.png + User user = hostHolder.getUser(); + String headUrl = domain + contextPath + "/user/header/" + fileName; + userService.updateHeader(user.getId(), headUrl); + + return "redirect:/index"; + + } + + /** + * 响应/显示用户上传的图片文件 + * 即解析当前用户头像的路径(web 访问路径)http://localhost:8080/echo/user/header/fileName + * @param fileName + * @param response + */ + @GetMapping("/header/{fileName}") + public void getHeader(@PathVariable("fileName") String fileName, HttpServletResponse response) { + // 图片存放在服务器上的路径 + fileName = uploadPath + "/" + fileName; + // 图片文件后缀名 + String suffix = fileName.substring(fileName.lastIndexOf(".")); + // 响应图片 + response.setContentType("image/" + suffix); + try ( + FileInputStream fis = new FileInputStream(fileName); + OutputStream os = response.getOutputStream(); + ) { + byte[] buffer = new byte[1024]; + int b = 0; + while ((b = fis.read(buffer)) != -1) { + os.write(buffer, 0, b); + } + } catch (IOException e) { + logger.error("读取文件失败" + e.getMessage()); + } + } +``` + +其中: + +```java +try ( + FileInputStream fis = new FileInputStream(fileName); + OutputStream os = response.getOutputStream(); +) { + +} +``` + +这样的写法的好处就是:写在括号里的流会被自动的关闭,不用我们在finally 里面手写关闭语句 + +### 前端 + +```html +