• 1.配置类
  • 2.文件上传工具类
  • 3.前端页面
  • 4.控制层:

1.配置类

# 项目相关配置
ruoyi:# 名称name: RuoYi# 版本version: 4.6.2# 版权年份copyrightYear: 2021# 实例演示开关demoEnabled: true# 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath)profile: C:\Users\14172\AppData\Local# 获取ip地址开关addressEnabled: false
/*** 全局配置类* * @author ruoyi*/
@Component
@ConfigurationProperties(prefix = "ruoyi")
public class RuoYiConfig
{/** 项目名称 */private static String name;/** 版本 */private static String version;/** 版权年份 */private static String copyrightYear;/** 实例演示开关 */private static boolean demoEnabled;/** 上传路径 */private static String profile;/** 获取地址开关 */private static boolean addressEnabled;public static String getName(){return name;}public void setName(String name){RuoYiConfig.name = name;}public static String getVersion(){return version;}public void setVersion(String version){RuoYiConfig.version = version;}public static String getCopyrightYear(){return copyrightYear;}public void setCopyrightYear(String copyrightYear){RuoYiConfig.copyrightYear = copyrightYear;}public static boolean isDemoEnabled(){return demoEnabled;}public void setDemoEnabled(boolean demoEnabled){RuoYiConfig.demoEnabled = demoEnabled;}public static String getProfile(){return profile;}public void setProfile(String profile){RuoYiConfig.profile = profile;}public static boolean isAddressEnabled(){return addressEnabled;}public void setAddressEnabled(boolean addressEnabled){RuoYiConfig.addressEnabled = addressEnabled;}/*** 获取导入上传路径*/public static String getImportPath(){return getProfile() + "/import";}/*** 获取头像上传路径*/public static String getAvatarPath(){return getProfile() + "/avatar";}/*** 获取下载路径*/public static String getDownloadPath(){return getProfile() + "/download/";}/*** 获取上传路径*/public static String getUploadPath(){return getProfile() + "/upload";}
}

2.文件上传工具类


/*** 文件上传工具类* * @author ruoyi*/
public class FileUploadUtils
{/*** 默认大小 50M*/public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024;/*** 默认的文件名最大长度 100*/public static final int DEFAULT_FILE_NAME_LENGTH = 100;/*** 默认上传的地址*/private static String defaultBaseDir = RuoYiConfig.getProfile();public static void setDefaultBaseDir(String defaultBaseDir){FileUploadUtils.defaultBaseDir = defaultBaseDir;}public static String getDefaultBaseDir(){return defaultBaseDir;}/*** 以默认配置进行文件上传** @param file 上传的文件* @return 文件名称* @throws Exception*/public static final String upload(MultipartFile file) throws IOException{try{return upload(getDefaultBaseDir(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);}catch (Exception e){throw new IOException(e.getMessage(), e);}}/*** 根据文件路径上传** @param baseDir 相对应用的基目录* @param file 上传的文件* @return 文件名称* @throws IOException*/public static final String upload(String baseDir, MultipartFile file) throws IOException{try{return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);}catch (Exception e){throw new IOException(e.getMessage(), e);}}/*** 文件上传** @param baseDir 相对应用的基目录* @param file 上传的文件* @param allowedExtension 上传文件类型* @return 返回上传成功的文件名* @throws FileSizeLimitExceededException 如果超出最大大小* @throws FileNameLengthLimitExceededException 文件名太长* @throws IOException 比如读写文件出错时* @throws InvalidExtensionException 文件校验异常*/public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,InvalidExtensionException{int fileNamelength = file.getOriginalFilename().length();if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH){throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);}assertAllowed(file, allowedExtension);String fileName = extractFilename(file);File desc = getAbsoluteFile(baseDir, fileName);file.transferTo(desc);String pathFileName = getPathFileName(baseDir, fileName);return pathFileName;}/*** 编码文件名*/public static final String extractFilename(MultipartFile file){String fileName = file.getOriginalFilename();String extension = getExtension(file);fileName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension;return fileName;}public static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException{File desc = new File(uploadDir + File.separator + fileName);if (!desc.exists()){if (!desc.getParentFile().exists()){desc.getParentFile().mkdirs();}}return desc;}public static final String getPathFileName(String uploadDir, String fileName) throws IOException{int dirLastIndex = RuoYiConfig.getProfile().length() + 1;String currentDir = StringUtils.substring(uploadDir, dirLastIndex);String pathFileName = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;return pathFileName;}/*** 文件大小校验** @param file 上传的文件* @return* @throws FileSizeLimitExceededException 如果超出最大大小* @throws InvalidExtensionException*/public static final void assertAllowed(MultipartFile file, String[] allowedExtension)throws FileSizeLimitExceededException, InvalidExtensionException{long size = file.getSize();if (DEFAULT_MAX_SIZE != -1 && size > DEFAULT_MAX_SIZE){throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);}String fileName = file.getOriginalFilename();String extension = getExtension(file);if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)){if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION){throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,fileName);}else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION){throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,fileName);}else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION){throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,fileName);}else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION){throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension,fileName);}else{throw new InvalidExtensionException(allowedExtension, extension, fileName);}}}/*** 判断MIME类型是否是允许的MIME类型** @param extension* @param allowedExtension* @return*/public static final boolean isAllowedExtension(String extension, String[] allowedExtension){for (String str : allowedExtension){if (str.equalsIgnoreCase(extension)){return true;}}return false;}/*** 获取文件名的后缀* * @param file 表单文件* @return 后缀名*/public static final String getExtension(MultipartFile file){String extension = FilenameUtils.getExtension(file.getOriginalFilename());if (StringUtils.isEmpty(extension)){extension = MimeTypeUtils.getExtension(file.getContentType());}return extension;}
}

3.前端页面

<body class="white-bg"><div class="row container"><div class="col-md-10"><div class="imageBox"><img id="avatar" th:src="(${#strings.isEmpty(user.avatar)}) ? @{/img/profile.jpg} : @{${user.avatar}}" th:onerror="'this.src=\'' + @{'/img/profile.jpg'} + '\''"></div><div class="action"><div class="new-contentarea tc"><a href="javascript:void(0)" class="upload-img"><label for="inputImage">上传图像</label> </a><input type="file" name="avatar" id="inputImage" accept="image/*"/></div><button type="button" class="btn-custom" data-method="zoom" data-option="0.1"><i class="fa fa-search-plus"></i></button><button type="button" class="btn-custom" data-method="zoom" data-option="-0.1"><i class="fa fa-search-minus"></i></button><button type="button" class="btn-custom" data-method="rotate" data-option="-45"><i class="fa fa-rotate-left"></i></button><button type="button" class="btn-custom" data-method="rotate" data-option="45"><i class="fa fa-rotate-right"></i></button><button type="button" class="btn-custom" data-method="scaleX" data-option="-1"><i class="fa fa-arrows-h"></i></button><button type="button" class="btn-custom" data-method="scaleY" data-option="-1"><i class="fa fa-arrows-v"></i></button><button type="button" class="btn-custom" data-method="reset"><i class="fa fa-refresh"></i></button></div></div><div class="col-md-2"><div class="cropped"><div class="preview-box"><div class="img-preview preview-xs"></div></div><div class="preview-box"><div class="img-preview preview-sm"></div></div><div class="preview-box"><div class="img-preview preview-md"></div></div></div></div></div>
<script type="text/javascript">
var cropper;
var croppable = false;
$(window).load(function() {var image = document.getElementById('avatar');cropper = new Cropper(image, {aspectRatio: 1,viewMode: 1,autoCropArea: 0.9,preview: '.img-preview',ready: function () {croppable = true;}})$('#inputImage').on('change', function() {var reader = new FileReader();var file = $('#inputImage')[0].files[0];if (/^image\/\w+$/.test(file.type)) {reader.onload = function(e) {if(croppable){cropper.replace(e.target.result)}}reader.readAsDataURL(this.files[0]);} else {$.modal.alertWarning('请选择一个图片文件。');}});$('.btn-custom').on('click',function (e) {if (!croppable) {$.modal.alertWarning("裁剪框加载中,请稍后...");return;}var data = {method: $(this).data('method'),option: $(this).data('option') || undefined,};var result = cropper[data.method](data.option, data.secondOption);if(['scaleX','scaleY'].indexOf(data.method) !== -1){$(this).data('option', -data.option)}})
});function submitHandler() {if (!croppable) {$.modal.alertWarning("裁剪框加载中,请稍后...");return}cropper.getCroppedCanvas().toBlob(function(img) {var formdata = new FormData();formdata.append("avatarfile", img);$.ajax({url: ctx + "system/user/profile/updateAvatar",data: formdata,type: "post",processData: false,contentType: false,success: function(result) {$.operate.saveSuccess(result);}})});
}$(window).resize(function() {$('.imageBox').height($(window).height() - 80);$('.cropped').height($(window).height() - 40);
}).resize();if (!HTMLCanvasElement.prototype.toBlob) {Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', {value: function(callback, type, quality) {var canvas = this;setTimeout(function() {var binStr = atob(canvas.toDataURL(type, quality).split(',')[1]);var len = binStr.length;var arr = new Uint8Array(len);for (var i = 0; i < len; i++) {arr[i] = binStr.charCodeAt(i);}callback(new Blob([arr], {type: type || 'image/png'}));});}});
}
</script>
</body>

4.控制层:

   @PostMapping("/updateAvatar")@ResponseBodypublic AjaxResult updateAvatar(@RequestParam("avatarfile") MultipartFile file){SysUser currentUser = getSysUser();try{if (!file.isEmpty()){System.out.println(RuoYiConfig.getAvatarPath());String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);currentUser.setAvatar(avatar);if (userService.updateUserInfo(currentUser) > 0){setSysUser(userService.selectUserById(currentUser.getUserId()));return success();}}return error();}catch (Exception e){log.error("修改头像失败!", e);return error(e.getMessage());}}

效果

springboot项目修改个人头像相关推荐

  1. SpringBoot项目修改html后不即时编译

    SpringBoot项目修改html后不即时编译 springboot templates 下的 html 修改后无法达到即时编译的效果,搜索资料后记录笔记. 原文地址:https://www.cnb ...

  2. Springboot项目修改文件传输(minio)限制大小

    Springboot项目修改文件传输(minio)限制大小 nginx 配置文件 springboot 项目配置文件 公司文件管理服务使用的 minio,很方便,也很快捷. 有天新来小同事说,mini ...

  3. springboot项目上传头像回显

    springboot项目使用ajax上传头像回显 先上效果图: 开始上传 点击提交 环境: springboot2.2.5.RELEASE jdk:1.8 thymeleaf:3.0.4.RELEAS ...

  4. SpringBoot项目修改JDK后报错(class file version 55.0)

    项目中更换JDK,由jdk9换到jdk11 报错信息 java.lang.UnsupportedClassVersionError: org/zjh/openlayersdemo/Openlayers ...

  5. eclispe Springboot项目修改html,jsp 页面不能及时刷新

    Springboot静态文件不更新的解决办法,以及Springboot实现热部署 正确答案是把菜单 Project > Build Automatically .(之前不知道怎么手瞎把这个给去了 ...

  6. SpringBoot 项目修改html后不需要重新启动(热部署)

    基于IDEA配置: 一.引入依赖 <dependency><groupId>org.springframework.boot</groupId><artifa ...

  7. idea springboot项目修改代码后刷新启动,不用重新启动

    此种方法只适用于修改类及方法,配置类文件,注解仍需重启

  8. Springboot项目修改html后不需要重启---springboot项目的热部署

    一.spring-boot-devtools 在pom中直接引入依赖 <dependency><groupId>org.springframework.boot</gro ...

  9. idea中springBoot项目修改html之类的文件后服务不自动更新

    打开File–>Settings–>Build,Execution,Deployment–>Compiler,勾选Build project automatically 组合键:&q ...

最新文章

  1. mysql之基础操作grant、show、repair、log_error等
  2. 安卓开发学习日记 DAY5——监听事件onClick的实现方法
  3. Java泛型应用详解
  4. oracle--pl/sql变量定义----
  5. 全面解析RS232、RS485、RS422、RJ45接口的区别和各自的应用
  6. iconpath 微信小程序_【报Bug】微信小程序 map 标记点iconPath图标 苹果手机 不能单个设置了。以前没有问题。现在不知道为啥不行了...
  7. 5.5 准备创建bean
  8. Docker入门-安装
  9. 表达式目录树(Expression)
  10. 在线编程语言模拟(Java,C,Python,R语言,Ruby,PHP,Perl,Go等)
  11. SQL之间,MySQL在日期之间,而不是之间
  12. 04748JAVA语言程序设计实践考试复习
  13. 赛事+内容IP齐发力,汽车之家打破Z世代次元壁
  14. 【java】PageHelper.startPage
  15. 贴片铝电容识别及型号_如何识别各种材质贴片电容
  16. centos6 yum安装nginx
  17. @Value的用法 @Value数据来源 @Value动态刷新的问题
  18. springcloud采坑-jason序列化中的Date对象
  19. php的rsa密匙长度,PHP解决RSA公私密钥换行处理
  20. 安卓 post请求与回调(绑接口)

热门文章

  1. 电子测量与仪器第四版pdf_固定资产管理系统_资产分类名称(电子和通信测量分析仪器篇)...
  2. 优学院java架构52破解_[单选] 肢体根据需要采用气囊止血带上肢压力至()
  3. WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
  4. c语言计算机二级考试内容,2017计算机二级考试内容C语言
  5. 计算机二级c选择题题库,C程序设计选择题题库【2018计算机二级考试题库:《C++》选择题练习】...
  6. python remove函数_python中remove函数的用法是什么?
  7. 四十七、Ansible自动化入门
  8. 阿里广告技术最新突破:全链路联动-面向最终目标的全链路一致性建模
  9. 博士申请 | 北京大学信息科学技术学院段凌宇教授招收2022级博士生
  10. Transformer升级之路:博采众长的旋转式位置编码