Skip to content

Commit 3694add

Browse files
committed
文件上传优化,加入FileProperties,根据系统选择上传目录
1 parent 646a795 commit 3694add

6 files changed

Lines changed: 78 additions & 27 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package me.zhengjie.config;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Data
8+
@Configuration
9+
@ConfigurationProperties(prefix = "file")
10+
public class FileProperties {
11+
12+
/** 文件大小限制 */
13+
private Long maxSize;
14+
15+
/** 头像大小限制 */
16+
private Long avatarMaxSize;
17+
18+
private ElPath mac;
19+
20+
private ElPath linux;
21+
22+
private ElPath windows;
23+
24+
public ElPath getPath(){
25+
String os = System.getProperty("os.name");
26+
if(os.toLowerCase().startsWith("win")) {
27+
return windows;
28+
} else if(os.toLowerCase().startsWith("mac")){
29+
return mac;
30+
}
31+
return linux;
32+
}
33+
34+
@Data
35+
public static class ElPath{
36+
37+
private String path;
38+
39+
private String avatar;
40+
}
41+
}

eladmin-system/src/main/java/me/zhengjie/config/ConfigurerAdapter.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package me.zhengjie.config;
22

3-
import org.springframework.beans.factory.annotation.Value;
43
import org.springframework.context.annotation.Bean;
54
import org.springframework.context.annotation.Configuration;
65
import org.springframework.web.cors.CorsConfiguration;
@@ -20,11 +19,12 @@
2019
@EnableWebMvc
2120
public class ConfigurerAdapter implements WebMvcConfigurer {
2221

23-
@Value("${file.path}")
24-
private String path;
22+
/** 文件配置 */
23+
private final FileProperties properties;
2524

26-
@Value("${file.avatar}")
27-
private String avatar;
25+
public ConfigurerAdapter(FileProperties properties) {
26+
this.properties = properties;
27+
}
2828

2929
@Bean
3030
public CorsFilter corsFilter() {
@@ -40,8 +40,9 @@ public CorsFilter corsFilter() {
4040

4141
@Override
4242
public void addResourceHandlers(ResourceHandlerRegistry registry) {
43-
String avatarUtl = "file:" + avatar.replace("\\","/");
44-
String pathUtl = "file:" + path.replace("\\","/");
43+
FileProperties.ElPath path = properties.getPath();
44+
String avatarUtl = "file:" + path.getAvatar().replace("\\","/");
45+
String pathUtl = "file:" + path.getPath().replace("\\","/");
4546
registry.addResourceHandler("/avatar/**").addResourceLocations(avatarUtl).setCachePeriod(0);
4647
registry.addResourceHandler("/file/**").addResourceLocations(pathUtl).setCachePeriod(0);
4748
registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);

eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/UserServiceImpl.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.zhengjie.modules.system.service.impl;
22

3+
import me.zhengjie.config.FileProperties;
34
import me.zhengjie.modules.system.domain.User;
45
import me.zhengjie.exception.EntityExistException;
56
import me.zhengjie.exception.EntityNotFoundException;
@@ -12,7 +13,6 @@
1213
import me.zhengjie.modules.system.service.dto.UserQueryCriteria;
1314
import me.zhengjie.modules.system.service.mapper.UserMapper;
1415
import me.zhengjie.utils.*;
15-
import org.springframework.beans.factory.annotation.Value;
1616
import org.springframework.cache.annotation.CacheConfig;
1717
import org.springframework.cache.annotation.CacheEvict;
1818
import org.springframework.cache.annotation.Cacheable;
@@ -41,15 +41,13 @@ public class UserServiceImpl implements UserService {
4141
private final UserMapper userMapper;
4242
private final RedisUtils redisUtils;
4343
private final UserAvatarRepository userAvatarRepository;
44-
45-
@Value("${file.avatar}")
46-
private String avatar;
47-
48-
public UserServiceImpl(UserRepository userRepository, UserMapper userMapper, RedisUtils redisUtils, UserAvatarRepository userAvatarRepository) {
44+
private final FileProperties properties;
45+
public UserServiceImpl(UserRepository userRepository, UserMapper userMapper, RedisUtils redisUtils, UserAvatarRepository userAvatarRepository, FileProperties properties) {
4946
this.userRepository = userRepository;
5047
this.userMapper = userMapper;
5148
this.redisUtils = redisUtils;
5249
this.userAvatarRepository = userAvatarRepository;
50+
this.properties = properties;
5351
}
5452

5553
@Override
@@ -177,7 +175,7 @@ public void updateAvatar(MultipartFile multipartFile) {
177175
if(userAvatar != null){
178176
oldPath = userAvatar.getPath();
179177
}
180-
File file = FileUtil.upload(multipartFile, avatar);
178+
File file = FileUtil.upload(multipartFile, properties.getPath().getAvatar());
181179
assert file != null;
182180
userAvatar = userAvatarRepository.save(new UserAvatar(userAvatar,file.getName(), file.getPath(), FileUtil.getSize(multipartFile.getSize())));
183181
user.setUserAvatar(userAvatar);

eladmin-system/src/main/resources/config/application-dev.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,15 @@ swagger:
6565

6666
# 文件存储路径
6767
file:
68-
path: C:\eladmin\file\
69-
avatar: C:\eladmin\avatar\
68+
mac:
69+
path: ~/file/
70+
avatar: ~/avatar/
71+
linux:
72+
path: /home/eladmin/file/
73+
avatar: /home/eladmin/avatar/
74+
windows:
75+
path: C:\eladmin\file\
76+
avatar: C:\eladmin\avatar\
7077
# 文件大小 /M
7178
maxSize: 100
7279
avatarMaxSize: 5

eladmin-system/src/main/resources/config/application-prod.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,15 @@ swagger:
7474

7575
# 文件存储路径
7676
file:
77-
path: /home/eladmin/file/
78-
avatar: /home/eladmin/avatar/
77+
mac:
78+
path: ~/file/
79+
avatar: ~/avatar/
80+
linux:
81+
path: /home/eladmin/file/
82+
avatar: /home/eladmin/avatar/
83+
windows:
84+
path: C:\eladmin\file\
85+
avatar: C:\eladmin\avatar\
7986
# 文件大小 /M
8087
maxSize: 100
8188
avatarMaxSize: 5

eladmin-tools/src/main/java/me/zhengjie/service/impl/LocalStorageServiceImpl.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.zhengjie.service.impl;
22

33
import cn.hutool.core.util.ObjectUtil;
4+
import me.zhengjie.config.FileProperties;
45
import me.zhengjie.domain.LocalStorage;
56
import me.zhengjie.service.dto.LocalStorageDto;
67
import me.zhengjie.service.dto.LocalStorageQueryCriteria;
@@ -9,7 +10,6 @@
910
import me.zhengjie.utils.*;
1011
import me.zhengjie.repository.LocalStorageRepository;
1112
import me.zhengjie.service.LocalStorageService;
12-
import org.springframework.beans.factory.annotation.Value;
1313
import org.springframework.cache.annotation.CacheConfig;
1414
import org.springframework.cache.annotation.CacheEvict;
1515
import org.springframework.cache.annotation.Cacheable;
@@ -42,15 +42,12 @@ public class LocalStorageServiceImpl implements LocalStorageService {
4242

4343
private final LocalStorageMapper localStorageMapper;
4444

45-
@Value("${file.path}")
46-
private String path;
45+
private final FileProperties properties;
4746

48-
@Value("${file.maxSize}")
49-
private long maxSize;
50-
51-
public LocalStorageServiceImpl(LocalStorageRepository localStorageRepository, LocalStorageMapper localStorageMapper) {
47+
public LocalStorageServiceImpl(LocalStorageRepository localStorageRepository, LocalStorageMapper localStorageMapper, FileProperties properties) {
5248
this.localStorageRepository = localStorageRepository;
5349
this.localStorageMapper = localStorageMapper;
50+
this.properties = properties;
5451
}
5552

5653
@Override
@@ -78,10 +75,10 @@ public LocalStorageDto findById(Long id){
7875
@CacheEvict(allEntries = true)
7976
@Transactional(rollbackFor = Exception.class)
8077
public LocalStorageDto create(String name, MultipartFile multipartFile) {
81-
FileUtil.checkSize(maxSize, multipartFile.getSize());
78+
FileUtil.checkSize(properties.getMaxSize(), multipartFile.getSize());
8279
String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
8380
String type = FileUtil.getFileType(suffix);
84-
File file = FileUtil.upload(multipartFile, path + type + File.separator);
81+
File file = FileUtil.upload(multipartFile, properties.getPath().getPath() + type + File.separator);
8582
if(ObjectUtil.isNull(file)){
8683
throw new BadRequestException("上传失败");
8784
}

0 commit comments

Comments
 (0)