@RestController
@RequestMapping("upload")
public class UserController
extends BaseController {
@Autowired
private IUserService userService;
/**
* 存储上传的文件的文件夹名称
*/
public static final String UPLOAD_DIR = "upload";
/**
* 上传文件时允许上传的最大大小
*/
public static final long UPLOAD_MAX_SIZE
= 1 * 1024 * 1024;
/**
* 允许上传的文件类型的集合
*/
public static final List<String> UPLOAD_CONTENT_TYPES
= new ArrayList<String>();
/**
* 添加允许上传的文件类型
*/
static {
UPLOAD_CONTENT_TYPES.add("image/jpeg");
UPLOAD_CONTENT_TYPES.add("image/png");
UPLOAD_CONTENT_TYPES.add("image/gif");
UPLOAD_CONTENT_TYPES.add("image/bmp");
}
@PostMapping("change_avatar")
public ResponseResult<String> changeAvatar(
HttpServletRequest request,
@RequestParam("avatar") MultipartFile avatar) {
// 判断上传的文件是否为空:avatar.isEmpty()
if (avatar.isEmpty()) {
// 是:抛出异常:FileEmptyException
throw new FileEmptyException(
"上传文件失败!没有选择文件,或上传的文件为空!");
}
// 判断文件类型是否不在允许的范围内:avatar.getContentType() / list.contains(contentType)
String contentType = avatar.getContentType();
if (!UPLOAD_CONTENT_TYPES.contains(contentType)) {
// 是:抛出异常:FileContentTypeException
throw new FileContentTypeException(
"上传文件失败!不支持上传" + contentType + "类型的文件!");
}
// 判断文件大小是否超出了限制:avatar.getSize()
long size = avatar.getSize();
if (size > UPLOAD_MAX_SIZE) {
// 是:抛出异常:FileSizeException
throw new FileSizeException(
"上传文件失败!尝试上传的文件大小超出了限制!仅允许上传不超过" + UPLOAD_MAX_SIZE/1024/1024 + "M的文件!");
}
// 确定上传的文件夹
String parentPath = request.getServletContext().getRealPath(UPLOAD_DIR);
File parent = new File(parentPath);
if (!parent.exists()) {
parent.mkdirs();
}
// 确定上传的文件名
String orginalFilename = avatar.getOriginalFilename();
String suffix = "";
int beginIndex = orginalFilename.lastIndexOf(".");
if (beginIndex != -1) {
suffix = orginalFilename.substring(beginIndex);
}
String filename = UUID.randomUUID() + suffix;
// 执行存储
File dest = new File(parent, filename);
try {
avatar.transferTo(dest);
} catch (IOException e) {
// e.printStackTrace();
throw new FileUploadIOException(
"上传文件失败!出现读写错误,请联系系统管理员,或稍后再次尝试!");
}
// 将上传的文件路径存储到数据库:service.changeAvatar(uid, avatar)
Integer uid
= getUidFromSession(request.getSession());
String avatarUrl
= "/" + UPLOAD_DIR + "/" + filename;
userService.changeAvatar(uid, avatarUrl);
// 返回
ResponseResult<String> rr
= new ResponseResult<String>();
rr.setState(SUCCESS);
rr.setData(avatarUrl);
return rr;
}
}
SpringBoot上传图片
最新推荐文章于 2024-04-19 16:41:34 发布