avatar

目录
牛客网后端项目实战(十三):账号设置

本节内容:

上传文件

  • 请求:必须是POST请求
  • 表单:enctype=“multipart/form-data”
  • Spring MVC:通过 MultipartFile 处理上传文件

开发步骤

  • 上传头像
  • 获取头像

配置文件存储路径

首先上传头像得有一个存储头像的路径,这个路径不能是固定的,因为在本地开发和部署到服务器肯定不一样,目前存到本地,后期也会存到云服务器上。在application.properties里配上头像上传路径。

properties
1
2
community.path.domain=http://localhost:8080
community.path.upload=d:/workspace/communityData/upload

更新头像路径

还是从dao层向controller开发,由于头像直接存在本地,没有存到数据库,这里不涉及dao层。

service层主要处理user表里的headUrl,这个方法在userMapper里写过,直接调用就可以。

java
1
2
3
4
//修改头像路径
public int updateHeader(int userId,String headerUrl){
return userMapper.updateHeader(userId,headerUrl);
}

上传头像

这里用到的是Spring MVC的multipartFile,头像的存储和获取直接在controller层操作。新建一个UserController。

将类的访问路径设为/user。

java
1
2
3
@Controller
@RequestMapping("/user")
public class UserController {}

声明日志,注入我们配置好的上传路径,域名,项目路径,以及userService和hostHolder。

java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private static final Logger logger= LoggerFactory.getLogger(UserController.class);

@Value("${community.path.upload}")
private String uploadPath;

@Value("${community.path.domain}")
private String domain;

@Value("${server.servlet.context-path}")
private String contextPath;

@Autowired
private UserService userService;

@Autowired
private HostHolder hostHolder;

第一个方法,用于返回个人设置页面,直接返回模板路径。

java
1
2
3
4
@RequestMapping(path = "/setting",method = RequestMethod.GET)
public String getSettingPage(){
return "/site/setting";
}

然后是上传头像的方法,这里从容器获取两个对象,一个是MultiparFile,也就是从浏览器传过来的头像文件,一个是model,用于模型返回信息。

首先对空值进行处理,然后用substring分割出文件后缀,png或者jpg等等。如果没有后缀就提示文件格式不正确。对用户上传的图片重命名,用之前写的生成uuid的方法加上分割出来的文件后缀。

再在我们指定的文件存放位置新建一个文件,文件名使用生成的名字,并记录异常,将异常向上抛出,用于之后的处理。

然后从hostHolder里获取当前用户,更新头像路径。

java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@RequestMapping(path = "/upload",method = RequestMethod.POST)
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/community/user/header/xxx.png
User user=hostHolder.getUser();
String headerUrl=domain+contextPath+"/user/header/"+fileName;
userService.updateHeader(user.getId(),headerUrl);
return "redirect:/index";

}

头像获取,从访问路径中截取头像文件名,从容器获取response对象,用服务器存放的全路径覆盖文件名,使用文件输入流和response的输出流,建一个1024字节的缓冲区,从本地头像文件读取,输出到response里。

这里try(){}的写法是java7的语法,括号里的内容会自动在finally里关闭。

java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@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("."));
//响应图片
response.setContentType("/image"+suffix);
try (
FileInputStream fileInputStream=new FileInputStream(fileName);
OutputStream os=response.getOutputStream();
){

byte[] buffer=new byte[1024];
int b=0;
while ((b=fileInputStream.read(buffer))!=-1){
os.write(buffer,0,b);
}
} catch (IOException e) {
logger.error("读取头像失败"+e.getMessage());
}
}

运行项目,登录上传头像,可以看到这里显示的已经是我设置过的头像了。

文章作者: langsam
文章链接: https://langsam1998.github.io/2020/04/13/20200328-%E7%89%9B%E5%AE%A2%E7%BD%91%E5%90%8E%E7%AB%AF%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98%EF%BC%88%E5%8D%81%E4%B8%89%EF%BC%89%EF%BC%9A%E8%B4%A6%E5%8F%B7%E8%AE%BE%E7%BD%AE/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 平儿的博客
打赏
  • 微信
    微信
  • 支付寶
    支付寶

评论