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默认允许请求长度 ...
随机推荐
- windows.h头文件中改变光标位置的函数——SetConsoleCursorPosition
COORD 具体为 typedef struct COORD{ short X; short Y; } COORD,*PCOORD; 可以用来记录坐标. #include <iostre ...
- python 编写名字管理系统
1 #打印功能提示 2 print('='*50) 3 print(' 名字管理系统 v1.1') 4 print('1.添加新的名字') 5 print('2.删除一个名字') 6 print('3 ...
- go 继承
package main import "fmt" type Animal struct { Color string } // 继承动物结构体 type Dog struct { ...
- apache的ab压力测试介绍
1.ab命令原理 Apache的ab命令模拟多线程并发请求,测试服务器负载压力,也可以测试nginx.lighthttp.IIS等其它Web服务器的压力. ab命令对发出负载的计算机要求很低,既不会占 ...
- Python之tuple元组详解
元组:有序,一级元素不可以修改.不能被增加或删除(元组是可迭代对象) 一般写法括号内最后面加个英文逗号用来区分: test = (,) test1 = (11,22,) 例: test = (12 ...
- day74:drf:drf其他功能:认证/权限/限流/过滤/排序/分页/异常处理&自动生成接口文档
目录 1.django-admin 2.认证:Authentication 3.权限:Permissions 4.限流:Throttling 5.过滤:Filtering 6.排序:OrderingF ...
- 第二十七章 ansible变量介绍
一.ansible变量介绍 1.概念 变量提供了便捷的方式来管理Ansible playbook的每一个项目中的动态值,比如nginx-1.6.3这个软件包的版本,在其它地方或许会反复使用,那么如果讲 ...
- PHP代码审计04之strpos函数使用不当
前言 根据红日安全写的文章,学习PHP代码审计的第四节内容,题目均来自PHP SECURITY CALENDAR 2017,讲完题目会用一个实例来加深巩固,这是之前写的,有兴趣可以去看看: PHP代码 ...
- while语句的一个使用技巧
作用类似于可以输入一个不定量长度的数组,但是严格来说 不可能出现任意长度. 内存是有限的 超出一定长度后,不可能存的下,虽然这个值可能是超级大的,但总有限度. 这里我们利用while(cin>& ...
- Java学习的第十五天
1.今天复习了第四章的内容 重新看了看方法参数问题,final修饰的关键字 2.今天没问题 3.明天学习多态变化