Flask接口签名sign原理与实例代码浅析
452
2022-12-26
springboot上传文件过大的500异常解决
修改appliaction.properties
# 单个文件最大20m
spring.servlet.multipart.max-file-size=20MB
#一次请求最大100M
spring.servlet.multipart.max-request-size=100MB
如果配置文件为appliaction.yml的这样配置文件:
spring:
servlet:
multipart:
maxFileSize: 20MB
maxRequestSize: 100MB
500代码异常,在启动类的里追加
/**
* 文件上传配置
*
* @return
*/
@Bean
public MultipartConfigElement multipartConfigElement() {
/*
springBoot 1.0+ 推荐这种写法
MultipartConfigFactory factory = new MultipartConfigFactory();
//单个文件最大
factory.setMaxFileSize("20480KB"); //KB,MB
factory.setMaxFileSize("20480KB");
/// 设置总上传数据总大小
factory.setMaxRequestSize("102400KB");
return factory.createMultipartConfig();
*/
// springBoot 2.0+ 推荐这种写法,
MultipartConfigFactory factory = new MultipartConfigFactory();
//文件最大10M,DataUnit提供5中类型B,KB,MB,GB,TB
factory.setMaxFileSize(DataSize.of(10, DataUnit.MEGABYTES));
/// 设置总上传数据总大小10M
factory.setMaxRequestSize(DataSize.of(100, DataUnit.MEGABYTES));
return factory.createMultipartConfig();
}
这是我的启动类:
package com.tythin.tyboot.rest;
import org.apache.coyote.http11.AbstractHttp11Protocol;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annobFwDvGOSQtation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import com.tythin.tyboot.core.config.WebAutoConfiguration;
import javax.servlet.MultipartConfigElement;
@SpringBootApplication(exclude = { WebAutoConfiguration.class })
@EnableScheduling
bFwDvGOSQ@MapperScan(basePackages = "XXXX")
public class TybootRestApplication {
public static void main(String[] args) {
SpringApplication.run(TybootRestApplication.class, args);
}
@CbFwDvGOSQonfiguration
public class CorsConfig {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*"); // 1允许任何域名使用
corsConfiguration.addAllowedHeader("*"); // 2允许任何头
corsConfiguration.addAllowedMethod("*"); // 3允许任何方法(post、get等)
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig()); // 4
return new CorsFilter(source);
}
/**
* 文件上传配置
*
* @return
*/
@Bean
public MultipartConfigElement multipartConfigElement() {
/*
springBoot 1.0+ 推荐这种写法
MultipartConfigFactory factory = new MultipartConfigFactory();
//单个文件最大
factory.setMaxFileSize("20480KB"); //KB,MB
factory.setMaxFileSize("20480KB");
/// 设置总上传数据总大小
factory.setMaxRequestSize("102400KB");
return factory.createMultipartConfig();
*/
// springBoot 2.0+ 推荐这种写法,
MultipartConfigFactory factory = new MultipartConfigFactory();
//文件最大10M,DataUnit提供5中类型B,KB,MB,GB,TB
factory.setMaxFileSize(DataSize.of(10, DataUnit.MEGABYTES));
/// 设置总上传数据总大小10M
factory.setMaxRequestSize(DataSize.bFwDvGOSQof(100, DataUnit.MEGABYTES));
return factory.createMultipartConfig();
}
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~