【Java Web】用户账号设置

  • 上传文件
    • 请求:必须是POST请求
    • 表单:enctype=“multipart/form-data”
    • Spring MVC: 通过MultipartFile处理上传文件
  • 开发步骤
    • 访问账号设置界面
    • 上传头像
    • 获取头像

1. 访问账号设置界面

1.1 Controller

package com.nowcoder.community.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(path = "/user")
public class UserController {
    @RequestMapping(path = "/setting",method = RequestMethod.GET)
    public String getSettingPage(){
        return "/site/setting";
    }
}

1.2 配置好setting.html的设置和index.html中的跳转

2. 上传头像

2.1 全局配置

server.servlet.context-path=/community

# community
community.path.domain=http://localhost:8080
community.path.upload=/Users/katniss/Library/Mobile Documents/com~apple~CloudDocs/MyCode/nowcoder/upload

2.2 UserController

@RequestMapping(path="/upload", method = RequestMethod.POST)
    public String uploadHeader(MultipartFile headerImage, Model model){
        if(headerImage == null){
            model.addAttribute("headerMsg","您还没有选择图片!");
        }
        // 获取原始文件名及其后缀
        String fileName = headerImage.getOriginalFilename();  // 原始文件名
        String suffix = fileName.substring(fileName.lastIndexOf(".")); //从最后一个点往后截取
        if(StringUtils.isBlank(suffix)){
            model.addAttribute("headerMsg","文件格式不正确");
        }
        // 为了避免同名覆盖,需要给每个头像生成不同的名字
        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/communtiy/user/header/xxx.png
        User user = hostHolder.getUser();
        String headerUrl = domain + contextPath + "/user/header/" + fileName;
        userService.updateHeader(user.getId(), headerUrl);

        // 头像更新成功,返回首页
        return "redirect:/index";
    }

3. 获取头像

3.1 UserController

@RequestMapping(path = "/header/{filename}", method = RequestMethod.GET)
    public void getHeader(@PathVariable("filename") String fileName, HttpServletResponse response){
        // 服务器存放路径
        fileName = uploadPath + "/" + fileName;
        // 文件后缀
        String suffix = fileName.substring(fileName.lastIndexOf(".")+1);
        // 响应图片
        response.setContentType("image/"+suffix);
        try (
                OutputStream os = response.getOutputStream();  // 输出流
                FileInputStream fis = new FileInputStream(fileName);  // 输入流,读图片后才能输出
        ){
            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());
            throw new RuntimeException(e);
        }
    }

你可能感兴趣的:(java,java,spring)