<p>//1.优化修改头像接口代码
@PostMapping(&quot;/updateOneAdminByIdByFile&quot;)
public Result<Object> updateOneAdminByIdByFile(@RequestParam(value = &quot;file&quot;, required = false) MultipartFile file,
@RequestParam(value = &quot;adminId&quot;, required = false) Long adminId,
HttpServletRequest request) throws IOException {
//权限验证
String token = (String) request.getAttribute(&quot;claims_admin&quot;);
if (StringUtils.isEmpty(token)) {
log.info(&quot;token为空,权限不足!&quot;);
throw new RuntimeException(&quot;权限不足!&quot;);
}</p>
<pre><code>//查询需要修改的管理员对象
Admin admin = adminService.queryOneAdminByID(adminId);
if (admin == null) {
    log.info(&quot;没查到当前用户!&quot;);
    return new Result&lt;&gt;(ResultCode.FAIL);
}

//删除之前的头像
if (!StringUtils.isEmpty(admin.getAvatarUrl())) {
    String picName = StringUtils.substringAfterLast(admin.getAvatarUrl(), &quot;/&quot;);
    if (!&quot;1.jpg&quot;.equals(picName)) {
        String path = realPath + picName;
        File file1 = new File(path);
        if (!file1.exists()) {
            log.info(&quot;文件&quot; + path + &quot;不存在,删除失败!&quot;);
        } else if (file1.isFile()) {
            log.info(file1.delete() ? &quot;删除头像成功!&quot; : &quot;删除头像失败!&quot;);
        }
    }
}

//上传新头像
if (file == null || file.isEmpty()) {
    log.info(&quot;文件为空!&quot;);
    return new Result&lt;&gt;(ResultCode.FAIL);
}

String fileName = file.getOriginalFilename();
String type = StringUtils.substringAfterLast(fileName, &quot;.&quot;);
if (!StringUtils.equalsIgnoreCase(&quot;gif&quot;, type) &amp;&amp; !StringUtils.equalsIgnoreCase(&quot;png&quot;, type) &amp;&amp; !StringUtils.equalsIgnoreCase(&quot;jpg&quot;, type)) {
    log.info(&quot;不是我们想要的文件类型,请按要求重新上传&quot;);
    return new Result&lt;&gt;(ResultCode.FAIL);
}

// 自定义的文件名称
String pdNo = UUID.randomUUID().toString().replaceAll(&quot;-&quot;, &quot;&quot;);
String trueFileName = pdNo + &quot;-&quot; + LocalDate.now() + &quot;.jpg&quot;;
String path = realPath + trueFileName;
File dest = new File(path);
if (!dest.getParentFile().exists() &amp;&amp; !dest.getParentFile().mkdir()) {
    log.info(&quot;创建文件夹失败!&quot;);
    return new Result&lt;&gt;(ResultCode.FAIL);
}
file.transferTo(dest);

//更新管理员对象并保存
String avatarUrl = request.getScheme() + &quot;://&quot; + request.getServerName() + &quot;:&quot; + request.getServerPort() + &quot;/download/&quot; + trueFileName;
admin.setAvatarUrl(avatarUrl);
int ret = adminService.updateOneAdminByID(admin);
if (ret &gt; 0) {
    log.info(&quot;修改管理员成功!&quot; + admin);
    return new Result&lt;&gt;(ResultCode.SUCCESS);
} else {
    log.info(&quot;修改管理员失败!&quot;);
    return new Result&lt;&gt;(ResultCode.FAIL);
}
</code></pre>
<p>}</p>
<p>//2.代码优化说明
1)使用StringUtils工具类代替原来的判断字符串是否为空的方法,使代码更加简洁。
2)将删除头像和上传新头像的代码分开,提高代码的可读性和可维护性。
3)将获取当前日期的代码改为使用LocalDate.now(),简化代码。
4)将创建文件夹的代码逻辑改为一行代码,使代码更加简洁。
5)修改头像保存的方式,使其更加规范</p>
1修改头像 PostMappingupdateOneAdminByIdByFile public ResultObject updateOneAdminByIdByFileRequestParamvalue = filerequired = false MultipartFile file Reques

原文地址: http://www.cveoy.top/t/topic/gdVD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录