Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Eladmin
Commits
3694add6
Commit
3694add6
authored
Mar 28, 2020
by
ZhengJie
Browse files
文件上传优化,加入FileProperties,根据系统选择上传目录
parent
646a7953
Changes
6
Hide whitespace changes
Inline
Side-by-side
eladmin-common/src/main/java/me/zhengjie/config/FileProperties.java
0 → 100644
View file @
3694add6
package
me.zhengjie.config
;
import
lombok.Data
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.context.annotation.Configuration
;
@Data
@Configuration
@ConfigurationProperties
(
prefix
=
"file"
)
public
class
FileProperties
{
/** 文件大小限制 */
private
Long
maxSize
;
/** 头像大小限制 */
private
Long
avatarMaxSize
;
private
ElPath
mac
;
private
ElPath
linux
;
private
ElPath
windows
;
public
ElPath
getPath
(){
String
os
=
System
.
getProperty
(
"os.name"
);
if
(
os
.
toLowerCase
().
startsWith
(
"win"
))
{
return
windows
;
}
else
if
(
os
.
toLowerCase
().
startsWith
(
"mac"
)){
return
mac
;
}
return
linux
;
}
@Data
public
static
class
ElPath
{
private
String
path
;
private
String
avatar
;
}
}
eladmin-system/src/main/java/me/zhengjie/config/ConfigurerAdapter.java
View file @
3694add6
package
me.zhengjie.config
;
package
me.zhengjie.config
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.cors.CorsConfiguration
;
import
org.springframework.web.cors.CorsConfiguration
;
...
@@ -20,11 +19,12 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
...
@@ -20,11 +19,12 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@EnableWebMvc
@EnableWebMvc
public
class
ConfigurerAdapter
implements
WebMvcConfigurer
{
public
class
ConfigurerAdapter
implements
WebMvcConfigurer
{
@Value
(
"${file.path}"
)
/** 文件配置 */
private
String
path
;
private
final
FileProperties
properties
;
@Value
(
"${file.avatar}"
)
public
ConfigurerAdapter
(
FileProperties
properties
)
{
private
String
avatar
;
this
.
properties
=
properties
;
}
@Bean
@Bean
public
CorsFilter
corsFilter
()
{
public
CorsFilter
corsFilter
()
{
...
@@ -40,8 +40,9 @@ public class ConfigurerAdapter implements WebMvcConfigurer {
...
@@ -40,8 +40,9 @@ public class ConfigurerAdapter implements WebMvcConfigurer {
@Override
@Override
public
void
addResourceHandlers
(
ResourceHandlerRegistry
registry
)
{
public
void
addResourceHandlers
(
ResourceHandlerRegistry
registry
)
{
String
avatarUtl
=
"file:"
+
avatar
.
replace
(
"\\"
,
"/"
);
FileProperties
.
ElPath
path
=
properties
.
getPath
();
String
pathUtl
=
"file:"
+
path
.
replace
(
"\\"
,
"/"
);
String
avatarUtl
=
"file:"
+
path
.
getAvatar
().
replace
(
"\\"
,
"/"
);
String
pathUtl
=
"file:"
+
path
.
getPath
().
replace
(
"\\"
,
"/"
);
registry
.
addResourceHandler
(
"/avatar/**"
).
addResourceLocations
(
avatarUtl
).
setCachePeriod
(
0
);
registry
.
addResourceHandler
(
"/avatar/**"
).
addResourceLocations
(
avatarUtl
).
setCachePeriod
(
0
);
registry
.
addResourceHandler
(
"/file/**"
).
addResourceLocations
(
pathUtl
).
setCachePeriod
(
0
);
registry
.
addResourceHandler
(
"/file/**"
).
addResourceLocations
(
pathUtl
).
setCachePeriod
(
0
);
registry
.
addResourceHandler
(
"/**"
).
addResourceLocations
(
"classpath:/META-INF/resources/"
).
setCachePeriod
(
0
);
registry
.
addResourceHandler
(
"/**"
).
addResourceLocations
(
"classpath:/META-INF/resources/"
).
setCachePeriod
(
0
);
...
...
eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/UserServiceImpl.java
View file @
3694add6
package
me.zhengjie.modules.system.service.impl
;
package
me.zhengjie.modules.system.service.impl
;
import
me.zhengjie.config.FileProperties
;
import
me.zhengjie.modules.system.domain.User
;
import
me.zhengjie.modules.system.domain.User
;
import
me.zhengjie.exception.EntityExistException
;
import
me.zhengjie.exception.EntityExistException
;
import
me.zhengjie.exception.EntityNotFoundException
;
import
me.zhengjie.exception.EntityNotFoundException
;
...
@@ -12,7 +13,6 @@ import me.zhengjie.modules.system.service.dto.UserDto;
...
@@ -12,7 +13,6 @@ import me.zhengjie.modules.system.service.dto.UserDto;
import
me.zhengjie.modules.system.service.dto.UserQueryCriteria
;
import
me.zhengjie.modules.system.service.dto.UserQueryCriteria
;
import
me.zhengjie.modules.system.service.mapper.UserMapper
;
import
me.zhengjie.modules.system.service.mapper.UserMapper
;
import
me.zhengjie.utils.*
;
import
me.zhengjie.utils.*
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.cache.annotation.CacheConfig
;
import
org.springframework.cache.annotation.CacheConfig
;
import
org.springframework.cache.annotation.CacheEvict
;
import
org.springframework.cache.annotation.CacheEvict
;
import
org.springframework.cache.annotation.Cacheable
;
import
org.springframework.cache.annotation.Cacheable
;
...
@@ -41,15 +41,13 @@ public class UserServiceImpl implements UserService {
...
@@ -41,15 +41,13 @@ public class UserServiceImpl implements UserService {
private
final
UserMapper
userMapper
;
private
final
UserMapper
userMapper
;
private
final
RedisUtils
redisUtils
;
private
final
RedisUtils
redisUtils
;
private
final
UserAvatarRepository
userAvatarRepository
;
private
final
UserAvatarRepository
userAvatarRepository
;
private
final
FileProperties
properties
;
@Value
(
"${file.avatar}"
)
public
UserServiceImpl
(
UserRepository
userRepository
,
UserMapper
userMapper
,
RedisUtils
redisUtils
,
UserAvatarRepository
userAvatarRepository
,
FileProperties
properties
)
{
private
String
avatar
;
public
UserServiceImpl
(
UserRepository
userRepository
,
UserMapper
userMapper
,
RedisUtils
redisUtils
,
UserAvatarRepository
userAvatarRepository
)
{
this
.
userRepository
=
userRepository
;
this
.
userRepository
=
userRepository
;
this
.
userMapper
=
userMapper
;
this
.
userMapper
=
userMapper
;
this
.
redisUtils
=
redisUtils
;
this
.
redisUtils
=
redisUtils
;
this
.
userAvatarRepository
=
userAvatarRepository
;
this
.
userAvatarRepository
=
userAvatarRepository
;
this
.
properties
=
properties
;
}
}
@Override
@Override
...
@@ -177,7 +175,7 @@ public class UserServiceImpl implements UserService {
...
@@ -177,7 +175,7 @@ public class UserServiceImpl implements UserService {
if
(
userAvatar
!=
null
){
if
(
userAvatar
!=
null
){
oldPath
=
userAvatar
.
getPath
();
oldPath
=
userAvatar
.
getPath
();
}
}
File
file
=
FileUtil
.
upload
(
multipartFile
,
a
vatar
);
File
file
=
FileUtil
.
upload
(
multipartFile
,
properties
.
getPath
().
getA
vatar
()
);
assert
file
!=
null
;
assert
file
!=
null
;
userAvatar
=
userAvatarRepository
.
save
(
new
UserAvatar
(
userAvatar
,
file
.
getName
(),
file
.
getPath
(),
FileUtil
.
getSize
(
multipartFile
.
getSize
())));
userAvatar
=
userAvatarRepository
.
save
(
new
UserAvatar
(
userAvatar
,
file
.
getName
(),
file
.
getPath
(),
FileUtil
.
getSize
(
multipartFile
.
getSize
())));
user
.
setUserAvatar
(
userAvatar
);
user
.
setUserAvatar
(
userAvatar
);
...
...
eladmin-system/src/main/resources/config/application-dev.yml
View file @
3694add6
...
@@ -65,8 +65,15 @@ swagger:
...
@@ -65,8 +65,15 @@ swagger:
# 文件存储路径
# 文件存储路径
file
:
file
:
path
:
C:\eladmin\file\
mac
:
avatar
:
C:\eladmin\avatar\
path
:
~/file/
avatar
:
~/avatar/
linux
:
path
:
/home/eladmin/file/
avatar
:
/home/eladmin/avatar/
windows
:
path
:
C:\eladmin\file\
avatar
:
C:\eladmin\avatar\
# 文件大小 /M
# 文件大小 /M
maxSize
:
100
maxSize
:
100
avatarMaxSize
:
5
avatarMaxSize
:
5
\ No newline at end of file
eladmin-system/src/main/resources/config/application-prod.yml
View file @
3694add6
...
@@ -74,8 +74,15 @@ swagger:
...
@@ -74,8 +74,15 @@ swagger:
# 文件存储路径
# 文件存储路径
file
:
file
:
path
:
/home/eladmin/file/
mac
:
avatar
:
/home/eladmin/avatar/
path
:
~/file/
avatar
:
~/avatar/
linux
:
path
:
/home/eladmin/file/
avatar
:
/home/eladmin/avatar/
windows
:
path
:
C:\eladmin\file\
avatar
:
C:\eladmin\avatar\
# 文件大小 /M
# 文件大小 /M
maxSize
:
100
maxSize
:
100
avatarMaxSize
:
5
avatarMaxSize
:
5
\ No newline at end of file
eladmin-tools/src/main/java/me/zhengjie/service/impl/LocalStorageServiceImpl.java
View file @
3694add6
package
me.zhengjie.service.impl
;
package
me.zhengjie.service.impl
;
import
cn.hutool.core.util.ObjectUtil
;
import
cn.hutool.core.util.ObjectUtil
;
import
me.zhengjie.config.FileProperties
;
import
me.zhengjie.domain.LocalStorage
;
import
me.zhengjie.domain.LocalStorage
;
import
me.zhengjie.service.dto.LocalStorageDto
;
import
me.zhengjie.service.dto.LocalStorageDto
;
import
me.zhengjie.service.dto.LocalStorageQueryCriteria
;
import
me.zhengjie.service.dto.LocalStorageQueryCriteria
;
...
@@ -9,7 +10,6 @@ import me.zhengjie.exception.BadRequestException;
...
@@ -9,7 +10,6 @@ import me.zhengjie.exception.BadRequestException;
import
me.zhengjie.utils.*
;
import
me.zhengjie.utils.*
;
import
me.zhengjie.repository.LocalStorageRepository
;
import
me.zhengjie.repository.LocalStorageRepository
;
import
me.zhengjie.service.LocalStorageService
;
import
me.zhengjie.service.LocalStorageService
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.cache.annotation.CacheConfig
;
import
org.springframework.cache.annotation.CacheConfig
;
import
org.springframework.cache.annotation.CacheEvict
;
import
org.springframework.cache.annotation.CacheEvict
;
import
org.springframework.cache.annotation.Cacheable
;
import
org.springframework.cache.annotation.Cacheable
;
...
@@ -42,15 +42,12 @@ public class LocalStorageServiceImpl implements LocalStorageService {
...
@@ -42,15 +42,12 @@ public class LocalStorageServiceImpl implements LocalStorageService {
private
final
LocalStorageMapper
localStorageMapper
;
private
final
LocalStorageMapper
localStorageMapper
;
@Value
(
"${file.path}"
)
private
final
FileProperties
properties
;
private
String
path
;
@Value
(
"${file.maxSize}"
)
public
LocalStorageServiceImpl
(
LocalStorageRepository
localStorageRepository
,
LocalStorageMapper
localStorageMapper
,
FileProperties
properties
)
{
private
long
maxSize
;
public
LocalStorageServiceImpl
(
LocalStorageRepository
localStorageRepository
,
LocalStorageMapper
localStorageMapper
)
{
this
.
localStorageRepository
=
localStorageRepository
;
this
.
localStorageRepository
=
localStorageRepository
;
this
.
localStorageMapper
=
localStorageMapper
;
this
.
localStorageMapper
=
localStorageMapper
;
this
.
properties
=
properties
;
}
}
@Override
@Override
...
@@ -78,10 +75,10 @@ public class LocalStorageServiceImpl implements LocalStorageService {
...
@@ -78,10 +75,10 @@ public class LocalStorageServiceImpl implements LocalStorageService {
@CacheEvict
(
allEntries
=
true
)
@CacheEvict
(
allEntries
=
true
)
@Transactional
(
rollbackFor
=
Exception
.
class
)
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
LocalStorageDto
create
(
String
name
,
MultipartFile
multipartFile
)
{
public
LocalStorageDto
create
(
String
name
,
MultipartFile
multipartFile
)
{
FileUtil
.
checkSize
(
m
axSize
,
multipartFile
.
getSize
());
FileUtil
.
checkSize
(
properties
.
getM
axSize
()
,
multipartFile
.
getSize
());
String
suffix
=
FileUtil
.
getExtensionName
(
multipartFile
.
getOriginalFilename
());
String
suffix
=
FileUtil
.
getExtensionName
(
multipartFile
.
getOriginalFilename
());
String
type
=
FileUtil
.
getFileType
(
suffix
);
String
type
=
FileUtil
.
getFileType
(
suffix
);
File
file
=
FileUtil
.
upload
(
multipartFile
,
p
ath
+
type
+
File
.
separator
);
File
file
=
FileUtil
.
upload
(
multipartFile
,
p
roperties
.
getPath
().
getPath
()
+
type
+
File
.
separator
);
if
(
ObjectUtil
.
isNull
(
file
)){
if
(
ObjectUtil
.
isNull
(
file
)){
throw
new
BadRequestException
(
"上传失败"
);
throw
new
BadRequestException
(
"上传失败"
);
}
}
...
...
Write
Preview
Supports
Markdown
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