Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
S
sts网站
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
liyang
sts网站
Commits
d46e62a2
Commit
d46e62a2
authored
Jun 06, 2025
by
RuoYi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
用户头像更换后移除旧头像文件
parent
fa889226
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
83 additions
and
38 deletions
+83
-38
CommonController.java
...ava/com/ruoyi/web/controller/common/CommonController.java
+1
-2
SysProfileController.java
...com/ruoyi/web/controller/system/SysProfileController.java
+10
-4
FileUploadUtils.java
...ain/java/com/ruoyi/common/utils/file/FileUploadUtils.java
+36
-8
FileUtils.java
.../src/main/java/com/ruoyi/common/utils/file/FileUtils.java
+13
-1
SysUserMapper.java
.../src/main/java/com/ruoyi/system/mapper/SysUserMapper.java
+4
-4
ISysUserService.java
...c/main/java/com/ruoyi/system/service/ISysUserService.java
+4
-4
SysUserServiceImpl.java
...ava/com/ruoyi/system/service/impl/SysUserServiceImpl.java
+6
-6
SysUserMapper.xml
...system/src/main/resources/mapper/system/SysUserMapper.xml
+9
-9
No files found.
ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java
View file @
d46e62a2
...
@@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
...
@@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartFile
;
import
com.ruoyi.common.config.RuoYiConfig
;
import
com.ruoyi.common.config.RuoYiConfig
;
import
com.ruoyi.common.constant.Constants
;
import
com.ruoyi.common.core.domain.AjaxResult
;
import
com.ruoyi.common.core.domain.AjaxResult
;
import
com.ruoyi.common.utils.StringUtils
;
import
com.ruoyi.common.utils.StringUtils
;
import
com.ruoyi.common.utils.file.FileUploadUtils
;
import
com.ruoyi.common.utils.file.FileUploadUtils
;
...
@@ -148,7 +147,7 @@ public class CommonController
...
@@ -148,7 +147,7 @@ public class CommonController
// 本地资源路径
// 本地资源路径
String
localPath
=
RuoYiConfig
.
getProfile
();
String
localPath
=
RuoYiConfig
.
getProfile
();
// 数据库资源地址
// 数据库资源地址
String
downloadPath
=
localPath
+
StringUtils
.
substringAfter
(
resource
,
Constants
.
RESOURCE_PREFIX
);
String
downloadPath
=
localPath
+
FileUtils
.
stripPrefix
(
resource
);
// 下载名称
// 下载名称
String
downloadName
=
StringUtils
.
substringAfterLast
(
downloadPath
,
"/"
);
String
downloadName
=
StringUtils
.
substringAfterLast
(
downloadPath
,
"/"
);
response
.
setContentType
(
MediaType
.
APPLICATION_OCTET_STREAM_VALUE
);
response
.
setContentType
(
MediaType
.
APPLICATION_OCTET_STREAM_VALUE
);
...
...
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysProfileController.java
View file @
d46e62a2
...
@@ -21,6 +21,7 @@ import com.ruoyi.common.utils.DateUtils;
...
@@ -21,6 +21,7 @@ import com.ruoyi.common.utils.DateUtils;
import
com.ruoyi.common.utils.SecurityUtils
;
import
com.ruoyi.common.utils.SecurityUtils
;
import
com.ruoyi.common.utils.StringUtils
;
import
com.ruoyi.common.utils.StringUtils
;
import
com.ruoyi.common.utils.file.FileUploadUtils
;
import
com.ruoyi.common.utils.file.FileUploadUtils
;
import
com.ruoyi.common.utils.file.FileUtils
;
import
com.ruoyi.common.utils.file.MimeTypeUtils
;
import
com.ruoyi.common.utils.file.MimeTypeUtils
;
import
com.ruoyi.framework.web.service.TokenService
;
import
com.ruoyi.framework.web.service.TokenService
;
import
com.ruoyi.system.service.ISysUserService
;
import
com.ruoyi.system.service.ISysUserService
;
...
@@ -94,7 +95,7 @@ public class SysProfileController extends BaseController
...
@@ -94,7 +95,7 @@ public class SysProfileController extends BaseController
String
oldPassword
=
params
.
get
(
"oldPassword"
);
String
oldPassword
=
params
.
get
(
"oldPassword"
);
String
newPassword
=
params
.
get
(
"newPassword"
);
String
newPassword
=
params
.
get
(
"newPassword"
);
LoginUser
loginUser
=
getLoginUser
();
LoginUser
loginUser
=
getLoginUser
();
String
userName
=
loginUser
.
getUsername
();
Long
userId
=
loginUser
.
getUserId
();
String
password
=
loginUser
.
getPassword
();
String
password
=
loginUser
.
getPassword
();
if
(!
SecurityUtils
.
matchesPassword
(
oldPassword
,
password
))
if
(!
SecurityUtils
.
matchesPassword
(
oldPassword
,
password
))
{
{
...
@@ -105,7 +106,7 @@ public class SysProfileController extends BaseController
...
@@ -105,7 +106,7 @@ public class SysProfileController extends BaseController
return
error
(
"新密码不能与旧密码相同"
);
return
error
(
"新密码不能与旧密码相同"
);
}
}
newPassword
=
SecurityUtils
.
encryptPassword
(
newPassword
);
newPassword
=
SecurityUtils
.
encryptPassword
(
newPassword
);
if
(
userService
.
resetUserPwd
(
user
Name
,
newPassword
)
>
0
)
if
(
userService
.
resetUserPwd
(
user
Id
,
newPassword
)
>
0
)
{
{
// 更新缓存用户密码&密码最后更新时间
// 更新缓存用户密码&密码最后更新时间
loginUser
.
getUser
().
setPwdUpdateDate
(
DateUtils
.
getNowDate
());
loginUser
.
getUser
().
setPwdUpdateDate
(
DateUtils
.
getNowDate
());
...
@@ -126,9 +127,14 @@ public class SysProfileController extends BaseController
...
@@ -126,9 +127,14 @@ public class SysProfileController extends BaseController
if
(!
file
.
isEmpty
())
if
(!
file
.
isEmpty
())
{
{
LoginUser
loginUser
=
getLoginUser
();
LoginUser
loginUser
=
getLoginUser
();
String
avatar
=
FileUploadUtils
.
upload
(
RuoYiConfig
.
getAvatarPath
(),
file
,
MimeTypeUtils
.
IMAGE_EXTENSION
);
String
avatar
=
FileUploadUtils
.
upload
(
RuoYiConfig
.
getAvatarPath
(),
file
,
MimeTypeUtils
.
IMAGE_EXTENSION
,
true
);
if
(
userService
.
updateUserAvatar
(
loginUser
.
getUser
name
(),
avatar
))
if
(
userService
.
updateUserAvatar
(
loginUser
.
getUser
Id
(),
avatar
))
{
{
String
oldAvatar
=
loginUser
.
getUser
().
getAvatar
();
if
(
StringUtils
.
isNotEmpty
(
oldAvatar
))
{
FileUtils
.
deleteFile
(
RuoYiConfig
.
getProfile
()
+
FileUtils
.
stripPrefix
(
oldAvatar
));
}
AjaxResult
ajax
=
AjaxResult
.
success
();
AjaxResult
ajax
=
AjaxResult
.
success
();
ajax
.
put
(
"imgUrl"
,
avatar
);
ajax
.
put
(
"imgUrl"
,
avatar
);
// 更新缓存用户头像
// 更新缓存用户头像
...
...
ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java
View file @
d46e62a2
...
@@ -13,11 +13,12 @@ import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
...
@@ -13,11 +13,12 @@ import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
import
com.ruoyi.common.exception.file.InvalidExtensionException
;
import
com.ruoyi.common.exception.file.InvalidExtensionException
;
import
com.ruoyi.common.utils.DateUtils
;
import
com.ruoyi.common.utils.DateUtils
;
import
com.ruoyi.common.utils.StringUtils
;
import
com.ruoyi.common.utils.StringUtils
;
import
com.ruoyi.common.utils.uuid.IdUtils
;
import
com.ruoyi.common.utils.uuid.Seq
;
import
com.ruoyi.common.utils.uuid.Seq
;
/**
/**
* 文件上传工具类
* 文件上传工具类
*
*
* @author ruoyi
* @author ruoyi
*/
*/
public
class
FileUploadUtils
public
class
FileUploadUtils
...
@@ -102,15 +103,35 @@ public class FileUploadUtils
...
@@ -102,15 +103,35 @@ public class FileUploadUtils
throws
FileSizeLimitExceededException
,
IOException
,
FileNameLengthLimitExceededException
,
throws
FileSizeLimitExceededException
,
IOException
,
FileNameLengthLimitExceededException
,
InvalidExtensionException
InvalidExtensionException
{
{
int
fileNamelength
=
Objects
.
requireNonNull
(
file
.
getOriginalFilename
()).
length
();
return
upload
(
baseDir
,
file
,
allowedExtension
,
false
);
if
(
fileNamelength
>
FileUploadUtils
.
DEFAULT_FILE_NAME_LENGTH
)
}
/**
* 文件上传
*
* @param baseDir 相对应用的基目录
* @param file 上传的文件
* @param useCustomNaming 系统自定义文件名
* @param allowedExtension 上传文件类型
* @return 返回上传成功的文件名
* @throws FileSizeLimitExceededException 如果超出最大大小
* @throws FileNameLengthLimitExceededException 文件名太长
* @throws IOException 比如读写文件出错时
* @throws InvalidExtensionException 文件校验异常
*/
public
static
final
String
upload
(
String
baseDir
,
MultipartFile
file
,
String
[]
allowedExtension
,
boolean
useCustomNaming
)
throws
FileSizeLimitExceededException
,
IOException
,
FileNameLengthLimitExceededException
,
InvalidExtensionException
{
int
fileNameLength
=
Objects
.
requireNonNull
(
file
.
getOriginalFilename
()).
length
();
if
(
fileNameLength
>
FileUploadUtils
.
DEFAULT_FILE_NAME_LENGTH
)
{
{
throw
new
FileNameLengthLimitExceededException
(
FileUploadUtils
.
DEFAULT_FILE_NAME_LENGTH
);
throw
new
FileNameLengthLimitExceededException
(
FileUploadUtils
.
DEFAULT_FILE_NAME_LENGTH
);
}
}
assertAllowed
(
file
,
allowedExtension
);
assertAllowed
(
file
,
allowedExtension
);
String
fileName
=
extractFilename
(
file
);
String
fileName
=
useCustomNaming
?
uuidFilename
(
file
)
:
extractFilename
(
file
);
String
absPath
=
getAbsoluteFile
(
baseDir
,
fileName
).
getAbsolutePath
();
String
absPath
=
getAbsoluteFile
(
baseDir
,
fileName
).
getAbsolutePath
();
file
.
transferTo
(
Paths
.
get
(
absPath
));
file
.
transferTo
(
Paths
.
get
(
absPath
));
...
@@ -118,12 +139,19 @@ public class FileUploadUtils
...
@@ -118,12 +139,19 @@ public class FileUploadUtils
}
}
/**
/**
* 编码文件名
* 编码文件名
(日期格式目录 + 原文件名 + 序列值 + 后缀)
*/
*/
public
static
final
String
extractFilename
(
MultipartFile
file
)
public
static
final
String
extractFilename
(
MultipartFile
file
)
{
{
return
StringUtils
.
format
(
"{}/{}_{}.{}"
,
DateUtils
.
datePath
(),
return
StringUtils
.
format
(
"{}/{}_{}.{}"
,
DateUtils
.
datePath
(),
FilenameUtils
.
getBaseName
(
file
.
getOriginalFilename
()),
Seq
.
getId
(
Seq
.
uploadSeqType
),
getExtension
(
file
));
FilenameUtils
.
getBaseName
(
file
.
getOriginalFilename
()),
Seq
.
getId
(
Seq
.
uploadSeqType
),
getExtension
(
file
));
}
/**
* 编编码文件名(日期格式目录 + UUID + 后缀)
*/
public
static
final
String
uuidFilename
(
MultipartFile
file
)
{
return
StringUtils
.
format
(
"{}/{}.{}"
,
DateUtils
.
datePath
(),
IdUtils
.
fastSimpleUUID
(),
getExtension
(
file
));
}
}
public
static
final
File
getAbsoluteFile
(
String
uploadDir
,
String
fileName
)
throws
IOException
public
static
final
File
getAbsoluteFile
(
String
uploadDir
,
String
fileName
)
throws
IOException
...
@@ -216,7 +244,7 @@ public class FileUploadUtils
...
@@ -216,7 +244,7 @@ public class FileUploadUtils
/**
/**
* 获取文件名的后缀
* 获取文件名的后缀
*
*
* @param file 表单文件
* @param file 表单文件
* @return 后缀名
* @return 后缀名
*/
*/
...
...
ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUtils.java
View file @
d46e62a2
...
@@ -11,13 +11,14 @@ import java.net.URLEncoder;
...
@@ -11,13 +11,14 @@ import java.net.URLEncoder;
import
java.nio.charset.StandardCharsets
;
import
java.nio.charset.StandardCharsets
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpServletResponse
;
import
org.apache.commons.io.FilenameUtils
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.commons.lang3.ArrayUtils
;
import
org.apache.commons.lang3.ArrayUtils
;
import
com.ruoyi.common.config.RuoYiConfig
;
import
com.ruoyi.common.config.RuoYiConfig
;
import
com.ruoyi.common.constant.Constants
;
import
com.ruoyi.common.utils.DateUtils
;
import
com.ruoyi.common.utils.DateUtils
;
import
com.ruoyi.common.utils.StringUtils
;
import
com.ruoyi.common.utils.StringUtils
;
import
com.ruoyi.common.utils.uuid.IdUtils
;
import
com.ruoyi.common.utils.uuid.IdUtils
;
import
org.apache.commons.io.FilenameUtils
;
/**
/**
* 文件处理工具类
* 文件处理工具类
...
@@ -103,6 +104,17 @@ public class FileUtils
...
@@ -103,6 +104,17 @@ public class FileUtils
return
FileUploadUtils
.
getPathFileName
(
uploadDir
,
pathName
);
return
FileUploadUtils
.
getPathFileName
(
uploadDir
,
pathName
);
}
}
/**
* 移除路径中的请求前缀片段
*
* @param filePath 文件路径
* @return 移除后的文件路径
*/
public
static
String
stripPrefix
(
String
filePath
)
{
return
StringUtils
.
substringAfter
(
filePath
,
Constants
.
RESOURCE_PREFIX
);
}
/**
/**
* 删除文件
* 删除文件
*
*
...
...
ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java
View file @
d46e62a2
...
@@ -70,20 +70,20 @@ public interface SysUserMapper
...
@@ -70,20 +70,20 @@ public interface SysUserMapper
/**
/**
* 修改用户头像
* 修改用户头像
*
*
* @param user
Name 用户名
* @param user
Id 用户ID
* @param avatar 头像地址
* @param avatar 头像地址
* @return 结果
* @return 结果
*/
*/
public
int
updateUserAvatar
(
@Param
(
"user
Name"
)
String
userName
,
@Param
(
"avatar"
)
String
avatar
);
public
int
updateUserAvatar
(
@Param
(
"user
Id"
)
Long
userId
,
@Param
(
"avatar"
)
String
avatar
);
/**
/**
* 重置用户密码
* 重置用户密码
*
*
* @param user
Name 用户名
* @param user
Id 用户ID
* @param password 密码
* @param password 密码
* @return 结果
* @return 结果
*/
*/
public
int
resetUserPwd
(
@Param
(
"user
Name"
)
String
userName
,
@Param
(
"password"
)
String
password
);
public
int
resetUserPwd
(
@Param
(
"user
Id"
)
Long
userId
,
@Param
(
"password"
)
String
password
);
/**
/**
* 通过用户ID删除用户
* 通过用户ID删除用户
...
...
ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java
View file @
d46e62a2
...
@@ -155,11 +155,11 @@ public interface ISysUserService
...
@@ -155,11 +155,11 @@ public interface ISysUserService
/**
/**
* 修改用户头像
* 修改用户头像
*
*
* @param user
Name 用户名
* @param user
Id 用户ID
* @param avatar 头像地址
* @param avatar 头像地址
* @return 结果
* @return 结果
*/
*/
public
boolean
updateUserAvatar
(
String
userName
,
String
avatar
);
public
boolean
updateUserAvatar
(
Long
userId
,
String
avatar
);
/**
/**
* 重置用户密码
* 重置用户密码
...
@@ -172,11 +172,11 @@ public interface ISysUserService
...
@@ -172,11 +172,11 @@ public interface ISysUserService
/**
/**
* 重置用户密码
* 重置用户密码
*
*
* @param user
Name 用户名
* @param user
Id 用户ID
* @param password 密码
* @param password 密码
* @return 结果
* @return 结果
*/
*/
public
int
resetUserPwd
(
String
userName
,
String
password
);
public
int
resetUserPwd
(
Long
userId
,
String
password
);
/**
/**
* 通过用户ID删除用户
* 通过用户ID删除用户
...
...
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java
View file @
d46e62a2
...
@@ -344,14 +344,14 @@ public class SysUserServiceImpl implements ISysUserService
...
@@ -344,14 +344,14 @@ public class SysUserServiceImpl implements ISysUserService
/**
/**
* 修改用户头像
* 修改用户头像
*
*
* @param user
Name 用户名
* @param user
Id 用户ID
* @param avatar 头像地址
* @param avatar 头像地址
* @return 结果
* @return 结果
*/
*/
@Override
@Override
public
boolean
updateUserAvatar
(
String
userName
,
String
avatar
)
public
boolean
updateUserAvatar
(
Long
userId
,
String
avatar
)
{
{
return
userMapper
.
updateUserAvatar
(
user
Name
,
avatar
)
>
0
;
return
userMapper
.
updateUserAvatar
(
user
Id
,
avatar
)
>
0
;
}
}
/**
/**
...
@@ -369,14 +369,14 @@ public class SysUserServiceImpl implements ISysUserService
...
@@ -369,14 +369,14 @@ public class SysUserServiceImpl implements ISysUserService
/**
/**
* 重置用户密码
* 重置用户密码
*
*
* @param user
Name 用户名
* @param user
Id 用户ID
* @param password 密码
* @param password 密码
* @return 结果
* @return 结果
*/
*/
@Override
@Override
public
int
resetUserPwd
(
String
userName
,
String
password
)
public
int
resetUserPwd
(
Long
userId
,
String
password
)
{
{
return
userMapper
.
resetUserPwd
(
user
Name
,
password
);
return
userMapper
.
resetUserPwd
(
user
Id
,
password
);
}
}
/**
/**
...
...
ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml
View file @
d46e62a2
...
@@ -19,13 +19,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
...
@@ -19,13 +19,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result
property=
"loginIp"
column=
"login_ip"
/>
<result
property=
"loginIp"
column=
"login_ip"
/>
<result
property=
"loginDate"
column=
"login_date"
/>
<result
property=
"loginDate"
column=
"login_date"
/>
<result
property=
"pwdUpdateDate"
column=
"pwd_update_date"
/>
<result
property=
"pwdUpdateDate"
column=
"pwd_update_date"
/>
<result
property=
"createBy"
column=
"create_by"
/>
<result
property=
"createBy"
column=
"create_by"
/>
<result
property=
"createTime"
column=
"create_time"
/>
<result
property=
"createTime"
column=
"create_time"
/>
<result
property=
"updateBy"
column=
"update_by"
/>
<result
property=
"updateBy"
column=
"update_by"
/>
<result
property=
"updateTime"
column=
"update_time"
/>
<result
property=
"updateTime"
column=
"update_time"
/>
<result
property=
"remark"
column=
"remark"
/>
<result
property=
"remark"
column=
"remark"
/>
<association
property=
"dept"
javaType=
"SysDept"
resultMap=
"deptResult"
/>
<association
property=
"dept"
javaType=
"SysDept"
resultMap=
"deptResult"
/>
<collection
property=
"roles"
javaType=
"java.util.List"
resultMap=
"RoleResult"
/>
<collection
property=
"roles"
javaType=
"java.util.List"
resultMap=
"RoleResult"
/>
</resultMap>
</resultMap>
<resultMap
id=
"deptResult"
type=
"SysDept"
>
<resultMap
id=
"deptResult"
type=
"SysDept"
>
...
@@ -202,11 +202,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
...
@@ -202,11 +202,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</update>
</update>
<update
id=
"updateUserAvatar"
parameterType=
"SysUser"
>
<update
id=
"updateUserAvatar"
parameterType=
"SysUser"
>
update sys_user set avatar = #{avatar} where user_
name = #{userName
}
update sys_user set avatar = #{avatar} where user_
id = #{userId
}
</update>
</update>
<update
id=
"resetUserPwd"
parameterType=
"SysUser"
>
<update
id=
"resetUserPwd"
parameterType=
"SysUser"
>
update sys_user set pwd_update_date = sysdate(), password = #{password} where user_
name = #{userName
}
update sys_user set pwd_update_date = sysdate(), password = #{password} where user_
id = #{userId
}
</update>
</update>
<delete
id=
"deleteUserById"
parameterType=
"Long"
>
<delete
id=
"deleteUserById"
parameterType=
"Long"
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment