springboot升级导致文件上传自动配置/tmp目录问题解决
1,..\web\src\main\resources\spring\web-men-applicationContext.xml
保留原有的bean配置
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- set the max upload size100MB -->
<property name="maxUploadSize">
<value>104857600</value>
</property>
</bean>
2,..\web\WebApplication.java
之前调试过程中,配置了本地bean信息,最终还是删除了,因为无法解决自己加目录/tmp问题
/**
* spring boot统一入口
*/
@ImportResource({"classpath:spring/web-men-applicationContext.xml"})
public class WebApplication extends MobileBaseApplication {
public static void main(String[] args) {
final ConfigurableApplicationContext context = SpringApplication.run(WebApplication.class, args);
}
@Bean
public FilterRegistrationBean characterEncodingfilter() {
OrderedCharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
filter.setEncoding("UTF-8");
filter.setForceEncoding(true);
filter.setForceRequestEncoding(true);
filter.setForceResponseEncoding(true);
FilterRegistrationBean registration = new FilterRegistrationBean(filter);
registration.addUrlPatterns("/*");
return registration;
}
@Bean
public FilterRegistrationBean corsFilterRegistration() {
CorsInterceptor filter = new CorsInterceptor();
FilterRegistrationBean registration = new FilterRegistrationBean(filter);
registration.setName("corsFilter");
registration.addUrlPatterns("/*");
return registration;
}
/**
* 文件上传配置-----最终还是去掉了
*/
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//单个文件最大
factory.setMaxFileSize("10240KB"); //KB,MB
/// 设置总上传数据总大小
factory.setMaxRequestSize("102400KB");
factory.setLocation("");
return factory.createMultipartConfig();
}
3,最终解决方式为:springboot基类中,
@EnableAutoConfiguration注解exclude里排除springboot自带的bean
MultipartAutoConfiguration.class
..\common\util\MobileBaseApplication.java
@Configuration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class
, DataSourceTransactionManagerAutoConfiguration.class, JndiDataSourceAutoConfiguration.class,
XADataSourceAutoConfiguration.class,MultipartAutoConfiguration.class})
public abstract class MobileBaseApplication extends QggBaseApplication {
}
下面来文字描述下问题的出现和解决思路:
1,@RequestParam("uploadFile") MultipartFile[] files,controller中files的size为0了。
解决方法:
移出掉web-men-applicationContext.xml中的
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- set the max upload size100MB -->
<property name="maxUploadSize">
<value>104857600</value>
</property>
</bean>
,这样就用org.springframework.web.multipart.support包下的transferTo方法,而不是原有自己配置的bean里org.springframework.web.multipart.commons包下的transferTo方法了。
但这引发了另外一个问题,上传文件的目录前被加了个/tmp目录,导致文件上传找不到真实路径。
最终write方法看到是org.eclipse.jetty.util包下MultiPartInputStreamParser类下的属性值 protected File _tmpDir;
this._file = new File(MultiPartInputStreamParser.this._tmpDir, fileName);
由于这么个东西的存在,决定把SpringBoot的统一入口中(WebApplication.java),添加bean,让spring初始化的时候去获取这个bean
/**
* 文件上传配置
*/
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//单个文件最大
factory.setMaxFileSize("10240KB"); //KB,MB
/// 设置总上传数据总大小
factory.setMaxRequestSize("102400KB");
factory.setLocation("");
return factory.createMultipartConfig();
}
但是还是不行,依然有/tmp出现,debug看到
MultiPartInputStreamParser.class下的parse()方法里this._tmpDir还是会被设置上默认值:
if (this._config.getLocation() == null) {
this._tmpDir = this._contextTmpDir;
} else if ("".equals(this._config.getLocation())) {
this._tmpDir = this._contextTmpDir;
} else {
File f = new File(this._config.getLocation());
if (f.isAbsolute()) {
this._tmpDir = f;
} else {
this._tmpDir = new File(this._contextTmpDir, this._config.getLocation());
}
}
因此认为,不能再让上传文件的transferTo再走到MultiPartInputStreamParser里面。
那么就还是要自己配置bean,而不是走springboot默认的配置。
所以把一切之前的针对文件上传改动还原到springboot升级之前。
要排除掉springboot的自动ioc,..\common.util\MobileBaseApplication.java
@Configuration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class
, DataSourceTransactionManagerAutoConfiguration.class, JndiDataSourceAutoConfiguration.class,
XADataSourceAutoConfiguration.class,MultipartAutoConfiguration.class})
public abstract class MobileBaseApplication extends QggBaseApplication {
}
springboot升级导致文件上传自动配置/tmp目录问题解决的更多相关文章
- SpringBoot项目实现文件上传和邮件发送
前言 本篇文章主要介绍的是SpringBoot项目实现文件上传和邮件发送的功能. SpringBoot 文件上传 说明:如果想直接获取工程那么可以直接跳到底部,通过链接下载工程代码. 开发准备 环境要 ...
- [转]ExtJs入门之filefield:文件上传的配置+结合Ajax完美实现文件上传的asp.net示例
原文地址:http://www.stepday.com/topic/?459 作文一个ExtJs的入门汉子,学习起来的确是比较费劲的事情,不过如今在这样一个网络资源如此丰富的时代,依然不是那么难了的. ...
- Springboot如何启用文件上传功能
网上的文章在写 "springboot文件上传" 时,都让你加上模版引擎,我只想说,我用不上,加模版引擎,你是觉得我脑子坏了,还是觉得我拿不动刀了. springboot如何启用文 ...
- springboot 文件上传大小配置
转自:https://blog.csdn.net/shi0299/article/details/69525848 springboot上传文件大小的配置有两种,一种是设置在配置文件里只有两行代码,一 ...
- SpringBoot之GZip压缩,HTTP/2,文件上传,缓存配置
1 设置应用端口以及context # HTTP Server port server.port=8080 # Make the application accessible on the given ...
- SpringBoot+BootStrap多文件上传到本地
1.application.yml文件配置 # 文件大小 MB必须大写 # maxFileSize 是单个文件大小 # maxRequestSize是设置总上传的数据大小 spring: servle ...
- asp.net文件/大文件上传需要配置的项目整理
HTTP 错误 404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求. 最可能的原因: •Web 服务器上的请求筛选被配置为拒绝该请求,因为内容长度超过配置的值. 可尝 ...
- php文件上传参考配置与大文件上传
PHP用超级全局变量数组$_FILES来记录文件上传相关信息的,在php文件上传之前,可通过调节php.ini中相关配置指令,来控制上传相关细节. 1.file_uploads=on/off ...
- WebService文件上传相关配置(404.13、超出限制、超时)
最近在做文件上传的功能,遇到一些问题,记录如下,以备以后使用. 1.HTTP Error 404.13 - Not Found,请求筛选模块被配置为拒绝超过请求内容长度的请求. IIS默认允许请求长度 ...
随机推荐
- C++ 虚函数简介!程序员必学知识,掌握编程从对象开始!
本文将简单探究一下 c++ 中的虚函数实现机制.主要基于 vs2013 生成的 32 位代码进行研究,相信其它编译器(比如, gcc )的实现大同小异. 先从对象大小开始 假设我们有如下代码,假设 i ...
- k8s 命令创建pod
[root@master kubernetes]# kubectl create deploy ngx-dep --image=nginx:1.14-alpine deployment.apps/ng ...
- 使用OLEDB方式 读取excel和csv文件
/// <summary> /// 使用OLEDB读取excel和csv文件 /// </summary> /// <param name="path" ...
- Python基础数据类型及其转换
数据类型: 浮点型float: 3.1415 也就是小数 整型 int: 123,主要用来运算,+-*/ 字符串 str: 'abc?', 记录少量信息 布尔值 bool: True or False ...
- jmeter环境变量配置
参考博客:超全 https://blog.csdn.net/qq_39720249/article/details/80721777
- MySQL设置慢查询
MySQL的慢查询日志是用来记录在MySQL中响应时间超过阀值的语句,则会被记录到慢查询日志中(运行时间超过long_query_time值的SQL语句): 慢查询相关参数: slow_query ...
- win7下安装docker
为了支持老版本的windows系统,docker官方提供了docker toolbox,让用户可以在windows10以前版本的操作系统上来体验docker. 一,安装 下载msi安装文件,一路nex ...
- 一、ETL实践之数据可视化架构
开篇心声: 不管是学习新知识,还是遇到各种难题,总能在技术论坛找到经验帖子.一直享受大家提供的帮助,而自己没有任何输出,实在过意不去.我相信技术是经验的交流,思维的碰撞. 这是我一次写技术分享文章,我 ...
- AngularJS——ui-router
深究AngularJS--ui-router详解 原创 2016年07月26日 13:45:14 标签: angularjs / ui-router / ui-view 25043 1.配置使用ui- ...
- SpringBoot整合JPA遇到的问题
在学习SpringBoot中使用Repository时出现这种错误 或者使用findOne也会报错,只需要改为 应该是SpringBoot版本的原因,fingOne()方法好像已经不用了.