欢迎访问自动发外链网站,大量seo自动发布外链工具与案例

SpringBoot + zyupload :优雅的实现网站文件上传,用户头像自定义

作者:jcmp      发布时间:2021-04-18      浏览量:0
1.开发前准备

1.开发前准备

commons-fileupload commons-fileupload 1.3.3

package com.ht.webfront.entity;import com.baomidou.mybatisplus.annotation.IdType;import java.util.Date;import com.baomidou.mybatisplus.annotation.Version;import com.baomidou.mybatisplus.annotation.TableId;import com.baomidou.mybatisplus.annotation.FieldFill;import com.baomidou.mybatisplus.annotation.TableLogic;import com.baomidou.mybatisplus.annotation.TableField;import java.io.Serializable;import io.swagger.annotations.ApiModel;import io.swagger.annotations.ApiModelProperty;import lombok.Data;import lombok.EqualsAndHashCode;import lombok.experimental.Accessors;/** *

* * 用户实体类 * *

* * * * @author 掌灬纹 * * @since 2021-03-11 */@Data@EqualsAndHashCode(callSuper = false)@Accessors(chain = true)@ApiModel(value="User对象", description="")public class User implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty(value = "自增id") @TableId(value = "id", type = IdType.AUTO) private Integer id; @ApiModelProperty(value = "用户编号") private String uid; @ApiModelProperty(value = "角色编号") private Integer roleId; @ApiModelProperty(value = "用户名") private String username; @ApiModelProperty(value = "密码") private String password; @ApiModelProperty(value = "头像") private String avatar; @ApiModelProperty(value = "登录时间") private Date loginDate; @ApiModelProperty(value = "创建时间") @TableField(fill = FieldFill.INSERT) private Date gmtCreate; @ApiModelProperty(value = "修改时间") @TableField(fill = FieldFill.INSERT_UPDATE) private Date gmtModified; @ApiModelProperty(value = "版本号") @Version private Integer version; @ApiModelProperty(value = "逻辑删除") @TableLogic private Integer deleted;}

2. 定义上传路径常量(常量信息提取)

package com.ht.webfront.persistence;/** * 资源路径枚举 */public class ResourceConst { /** * 资源存储根路径 */ public static final String ROOT_PATH = "D:\\resources\\"; // 视频文件 public static final String VIDEO = "video\\"; // 图片 public static final String IMAGE = "image\\"; // 文档 xls xlsx public static final String DOCUMENT = "document\\"; // avatar 特定头像存放位置 public static final String AVATAR = "avatar\\";}

3.编写简易前端页面 (含头像展示,zyupload文件上传插件即可,部分代码示例)。

头像上传:
仅支持 png jpg 格式单个图片上传,最大1MB(*^_^*)~

4.根据前端JS定义的上传插件处理 URL路径编写控制器(/user/avatarUpload)。

@Controllerpublic class UserController{ @ResponseBody @PostMapping("/user/avatarUpload") public String doUploadAvatar( HttpServletRequest request, HttpServletResponse response, @RequestParam("file") MultipartFile file ){ // 上传根路径 String rootPath = ResourceConst.ROOT_PATH + ResourceConst.AVATAR; //文件名 后缀名 String filename = file.getOriginalFilename(); String suffix = filename.substring(filename.lastIndexOf(".")); // 唯一文件名 String uidFile = CupidUtils.getUuid() + suffix; //System.out.println(rootPath + " : " + uidFile); // 图片上传 File target = new File(rootPath,uidFile); if (!target.exists()){ target.mkdirs(); } try { file.transferTo(target); // todo: 修改数据库头像位置信息 User user = (User) request.getSession().getAttribute(LoginUser.LOGIN_USER_SESSION); // 获取本地静态资源路径 String localPath = "/staticmv/avatar/" + uidFile; user.setAvatar(localPath); userService.updateById(user); return "头像上传成功"; } catch (IOException e) { e.printStackTrace(); } return "头像上传失败!"; }}

5. 最关键,配置SpringBoot 静态资源路径映射

package com.ht.webfront.config;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configurationpublic class SpringMVCConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/staticmv/**").addResourceLocations("file:///D:\\resources\\"); //addResourceHandlers(registry); }}

#对外暴露 静态资源接口file: uploadFloder: D:/resources/ staticAccessPath: /static/** common: uploadWindow: D:\resources\

6.可以测试了┗|`O′|┛ 嗷~~

(1).上传图片

(3). 刷新页面,展示成果 ( :

ps:文中相关资源可私信作者获取~