Spring上传文件路径配置

  1. 相对路径
spring.servlet.multipart.location=app/tmpfiles # 使用相对路径

注意没有./表示当前目录的写法
2. 绝对路径

spring.servlet.multipart.location=/tmp/app/tmpfiles # 使用绝对路径
  1. 默认地址,可不配
spring.servlet.multipart.location= ${java.io.tmpdir}
  1. 通过启动参数可以改变该行为;覆盖默认-Djava.io.tmpdir=/path/to/tmpdir
spring.servlet.multipart.location= ${java.io.tmpdir}

默认地址说明

  • Windows: C:\Users\{用户名}\AppData\Local\Temp
  • Solaris: /var/tmp/
  • Linux: /tmp
  • Mac OS X: /tmp

tomcat上传文件存储路径

// org.apache.catalina.connector.Request

File location;
// 获取配置的文件路径spring.servlet.multipart.location
String locationStr = mce.getLocation();
//为空时使用系统默认的临时文件路径
if (locationStr == null || locationStr.length() == 0) {
    location = ((File) context.getServletContext().getAttribute(
            ServletContext.TEMPDIR));
} else {
    // 如果是绝对路径直接使用;相对路径以临时文件夹为基准,自动创建对应路径的文件路径
    location = new File(locationStr);
    if (!location.isAbsolute()) {
        location = new File(
                (File) context.getServletContext().getAttribute(ServletContext.TEMPDIR),
                locationStr).getAbsoluteFile();
    }
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要限制Spring Boot应用程序中特定路径上传的文件大小,可以在应用程序的配置文件中添加以下配置: ``` spring.servlet.multipart.max-file-size=10MB spring.servlet.multipart.max-request-size=10MB ``` 上述配置会将上传的文件大小限制为10MB。 然后,为了限制特定路径下的上传文件大小,需要在应用程序中创建一个`MultipartConfigElement` bean,并将其应用于指定路径的控制器或方法。 例如,假设我们要限制路径`/upload`下的上传文件大小,我们可以在应用程序中添加以下代码: ```java @Configuration public class WebConfig { @Bean MultipartConfigElement multipartConfigElement() { long maxFileSize = 10 * 1024 * 1024; // 10MB long maxRequestSize = 10 * 1024 * 1024; int fileSizeThreshold = 0; return new MultipartConfigElement("/tmp", maxFileSize, maxRequestSize, fileSizeThreshold); } @Bean public WebMvcConfigurer webMvcConfigurer() { return new WebMvcConfigurerAdapter() { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new HandlerInterceptor() { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (request.getRequestURI().startsWith("/upload")) { request.setAttribute("org.apache.tomcat.websocket.DISPATCHER", true); request.setAttribute("multipartConfigElement", multipartConfigElement()); } return true; } }); } }; } } ``` 上述代码创建了一个`MultipartConfigElement` bean,并在`preHandle`方法中将其应用于路径以`/upload`开头的请求。 请注意,上述代码使用了过时的`WebMvcConfigurerAdapter`类。如果你使用的是Spring Boot 2.0或更高版本,则应该使用`WebMvcConfigurer`接口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值