devtools啟動(dòng)Springboot程序后,上傳文件到服務(wù)器引起程序重啟

通常我們?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);
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容