通常我们在写sprintgboot程序时,为了防止重复启动应用程序,都会加入热启动的相关配置。通常默认的配置对于我们大多数开发已经否用了。引入依赖如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>1.5.8.RELEASE</version>
<optional>true</optional>
</dependency>
但是,当我们在做上传文件到本地服务器,比如classpath目录后,由于devtools检测到classpath目录的文件发生了改变,便会自动重启,而此时的重启并不是我们需要的。所以我们便需要做如下配置,其目的是额外排除需要重启的目录,如下:static目录下的所有子目录和文件将被devtools额外排除。
spring:
devtools:
livereload:
enabled: true
restart:
additional-exclude: static/**
上传文件到本地服务器的接口一般采用POST接口,上传一个或者多个文件到本地服务器上的某个指定目录,并更改文件名。
@ApiOperation(value = "上传一个或多个文件到指定的文件夹内", notes = "/industries/mains/{industryId}/upload?folderName=xxx")
@PostMapping(value = "/mains/{industryId}/import")
public void upload(@PathVariable Integer industryId, @RequestPart("files") MultipartFile[] files, @RequestParam String folderName) throws Exception {
if (industryId == null || industryId <= 0) {
throw new BizException(BizExceptEnum.RecordNotExist);
}
if (files == null || files.length < 1) {
throw new BizException(BizExceptEnum.FileUploadZeroFile);
}
for (MultipartFile file : files) {
if (file == null) {
throw new BizException(BizExceptEnum.FileIsNull);
}
if (file.isEmpty()) {
throw new BizException(BizExceptEnum.FileIsEmpty);
}
String fileName = file.getOriginalFilename();
if (StringUtils.isEmpty(fileName)) {
throw new BizException(BizExceptEnum.FileEmptyFileName);
}
}
File classPath = new File(ResourceUtils.getURL(MsConstants.MS_CLASS_PATH).getPath());
if (!classPath.exists()) classPath = new File("");
String uploadPath = classPath.getAbsolutePath() + MsConstants.MS_STATIC_PATH + folderName;
FileUtil.getInstance().newFolder(uploadPath);
Map<String, String> map = new HashMap<>();
for (MultipartFile file : files) {
String fileName = file.getOriginalFilename();
String suffix = FileUtil.getInstance().getFileType(fileName);
String fileNameNonSuffix = FileUtil.getInstance().getFileNameNonSuffix(fileName);
String fileCode = industryId + "_" + fileNameNonSuffix + "_" + DateUtils.format(new Date(), DateUtils.FORMAT_FULL_ORDER_TOMIMUTE);
String ossFileName = fileCode + "." + suffix;
String uploadFullFileName = uploadPath + "/" + ossFileName;
File serverFile = new File(uploadFullFileName);
file.transferTo(serverFile);
}
}
网友评论