SpringBoot中使用yml配置文件以及配置类实现文件上传下载路径的修改

场景

SpringBoot+thymeleaf实现文件下载或者实现文件上传需要配置文件上传路径的地方,

不要写为固定路径,在配置文件中指定文件路径,代码中直接引用。

避免以后文件路径修改后需要修改业务代码。

SpringBoot+thymeleaf实现文件下载参照:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/88786370

实现

找到SpringBoot项目中的所使用的配置文件application,这里是开发环境,并且是yml文件。

SpringBoot中使用yml配置文件以及配置类实现文件上传下载路径的修改_第1张图片

添加如下自定义配置的值

# 自定义配置
ws:
  excelTemplateDpwloadPath: C:\release\sites\upload\excel\template

注意yml严格的缩进格式

注意excelTemplateDpwloadPath: 与C:\release\sites\upload\excel\template要有空格!!!

这里配置的路径就与要实现文件下的路径相对应。

SpringBoot中使用yml配置文件以及配置类实现文件上传下载路径的修改_第2张图片

 

新建config包并在config包下新建ConfigProperties.java

package com.ws.bus.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author badado
 * @version 1.0
 * @see
 **/
@Component
@ConfigurationProperties(prefix = ConfigProperties.PREFIX)
public class ConfigProperties {
    public static  final String PREFIX = "ws";
  
    private String excelTemplateDpwloadPath;

    public String getExcelTemplateDpwloadPath() {
        return excelTemplateDpwloadPath;
    }

    public void setExcelTemplateDpwloadPath(String excelTemplateDpwloadPath) {
        this.excelTemplateDpwloadPath = excelTemplateDpwloadPath;
    }


}

声明所使用的文件路径的属性名,并生成set和get方法。

在具体要使用的Controller中使用时:

@Controller
@RequestMapping("/wmsReceiveOrder")
@EnableConfigurationProperties(ConfigProperties.class)
public class WmsReceiveOrderController {

 @Autowired
 private ConfigProperties configProperties;

@Description("模板下载")
    @RequestMapping("/downloadOnlineLearnMaterials")
    public String downloadFile(HttpServletRequest request, HttpServletResponse response) {
        String fileName = "template.xlsx";// 设置文件名,根据业务需要替换成要下载的文件名
        if (fileName != null) {
            //设置文件路径
            String realPath = configProperties.getExcelTemplateDpwloadPath();//这里使用配置类配置文件路径
            File file = new File(realPath , fileName);
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    System.out.println("success");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return null;
    }
}

这样就可以通过

String realPath = configProperties.getExcelTemplateDpwloadPath();

获取设置的文件下载的路径,以后修改路径时直接可以通过配置文件修改。

你可能感兴趣的:(SpringBoot)