springBoot和SpringMVC上传头像代码案例

Controller:

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.tedu.store.controller.ex.FileEmptyException;
import cn.tedu.store.controller.ex.FileIOException;
import cn.tedu.store.controller.ex.FileSizeException;
import cn.tedu.store.controller.ex.FileStateException;
import cn.tedu.store.controller.ex.FileTypeException;
import cn.tedu.store.entity.User;
import cn.tedu.store.service.IUserService;
import cn.tedu.store.util.JsonResult;

@RestController
@RequestMapping("users")
public class UserController {

@Autowired
private IUserService service;



@RequestMapping("change_password")
public JsonResult<Void> changePassword(@RequestParam("old_password") String oldPassword, @RequestParam("new_password") String newPassword,HttpSession session){

// 从session中获取uid
Integer uid=Integer.valueOf(session.getAttribute(SESSION_UID).toString());

// 从session中获取username
String username=session.getAttribute(SESSION_USERNAME).toString();

service.changePassword(uid,oldPassword,newPassword,username);
return new JsonResult<Void>(SUCCESS);
}

private static final long AVATAR_MAX_SIZE=1*1024*1024;
private static final List<String> AVATAR_TYPES=new ArrayList<String>();

// 静态初始化器:用于初始化本类的静态成员
static {
AVATAR_TYPES.add("image/jpeg");
AVATAR_TYPES.add("image/png");
}


@PostMapping("change_avatar")
public JsonResult<String> changeAvatar(@RequestParam("file") MultipartFile file,
HttpServletRequest request,HttpSession session){

// 空文件验证
if(file.isEmpty()) {
throw new FileEmptyException("文件上传异常!文件不能为空");
}
// 文件大小验证
long fileSize=file.getSize();
if(fileSize>AVATAR_MAX_SIZE) {
throw new FileSizeException("文件上传异常!文件大小超过上限:"+(AVATAR_MAX_SIZE/1024)+"kb");
}

// 文件类型验证
if(!AVATAR_TYPES.contains(file.getContentType())) {
throw new FileTypeException("文件上传异常!文件类型不正确,允许的类型有:"+AVATAR_TYPES);
}


// 生成文件名
String oFilename=file.getOriginalFilename();
Integer index=oFilename.lastIndexOf(".");
String suffix="";
if(index!=-1) {
suffix=oFilename.substring(index);
}
String filename=UUID.randomUUID().toString()+suffix;

// 生成目标路径
String filePath=request.getServletContext().getRealPath("upload");
File parent=new File(filePath);
if(!parent.exists()) {
parent.mkdirs();//创建对应的目录
}

File dest=new File(parent,filename);
// 将用户上传的头像保存到服务器上
try {
file.transferTo(dest);
} catch (IllegalStateException e) {
throw new FileStateException("文件上传异常!"+e.getMessage());
} catch (IOException e) {
throw new FileIOException("文件上传异常!"+e.getMessage());
}

// 将头像在服务器的路径保存到数据库
String avatar="/upload/"+filename;
Integer uid=getUidFromSession(request.getSession());
String username=getUsernameFromSession(request.getSession());
service.changeAvatar(uid,avatar,username);
JsonResult<String> jr=new JsonResult<>(SUCCESS);
jr.setData(avatar);
return jr;
// return new JsonResult<String>(SUCCESS, avatar);
}
Service:
@Override
public void changeAvatar(Integer uid, String avatar, String modifiedUser)
throws UserNotFoundException, UpdateException {
// 使用uid查询用户数据
User user=mapper.findByUid(uid);
// 判断返回结果是否为null
if(user ==null) {
// 是:UserNotFoundException
throw new UserNotFoundException("上传头像异常!用户数据不存在");
}

// 判断isDelete是否为1
if(user.getIsDelete().equals(1)) {
// 是:UserNotFoundException
throw new UserNotFoundException("上传头像异常!用户数据不存在");
}

// 更新用户头像
Integer row=mapper.updateAvatar(uid, avatar, modifiedUser, new Date());
// 判断受影响的行数是否不为1
if(!row.equals(1)) {
// 是:UpdateException
throw new UpdateException("上传头像异常!请联系管理员");
}
}


posted @ 2020-12-29 14:26  小玲子丫  阅读(135)  评论(0编辑  收藏  举报