通常我們?cè)趯憇printgboot程序時(shí),為了防止重復(fù)啟動(dòng)應(yīng)用程序,都會(huì)加入熱啟動(dòng)的相關(guān)配置。通常默認(rèn)的配置對(duì)于我們大多數(shù)開發(fā)已經(jīng)否用了。引入依賴如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>1.5.8.RELEASE</version>
<optional>true</optional>
</dependency>
但是,當(dāng)我們?cè)谧錾蟼魑募奖镜胤?wù)器,比如classpath目錄后,由于devtools檢測(cè)到classpath目錄的文件發(fā)生了改變,便會(huì)自動(dòng)重啟,而此時(shí)的重啟并不是我們需要的。所以我們便需要做如下配置,其目的是額外排除需要重啟的目錄,如下:static目錄下的所有子目錄和文件將被devtools額外排除。
spring:
devtools:
livereload:
enabled: true
restart:
additional-exclude: static/**
上傳文件到本地服務(wù)器的接口一般采用POST接口,上傳一個(gè)或者多個(gè)文件到本地服務(wù)器上的某個(gè)指定目錄,并更改文件名。
@ApiOperation(value = "上傳一個(gè)或多個(gè)文件到指定的文件夾內(nèi)", 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);
}
}