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默认允许请求长度 ...
随机推荐
- Java NIO:选择器
最近打算把Java网络编程相关的知识深入一下(IO.NIO.Socket编程.Netty) Java NIO主要需要理解缓冲区.通道.选择器三个核心概念,作为对Java I/O的补充, 以提升大批量数 ...
- ansible:安装nginx1.18.0(使用role功能)
一,ansible使用role的用途? roles分别将变量/文件/任务/模板/handler等放置于单独的目录中, 并可以方便的include各目录下的功能 roles使playbook能实现代码被 ...
- ImageMagick实现图片的旋转/翻转/裁剪(ImageMagick6.9.10)
一,imagemagick的安装 请参见: https://www.cnblogs.com/architectforest/p/12807514.html 说明:刘宏缔的架构森林是一个专注架构的博客, ...
- css和xpath定位补充
定位的网页的地址:https://item.jd.com/100000323510.htmlxpath绝对定位: 1 以"/"开头的是绝对定位 2 /html/body/div[1 ...
- 第十八章 HTTPS介绍及实战演练
一.HTTPS介绍 1.概述 为什么需要使用HTTPS,因为HTTP不安全,当我们使用http网站时,会遭到劫持和篡改,如果采用https协议,那么数据在传输过程中是加密的,所以黑客无法窃取或者篡改数 ...
- 习题解答chapter04
题目: 实验:利用IDE的debug功能给例6.4和例6.6的new语句设置断点,使用单步调试(step into/step over)跟踪子类对象实例化(初始化)的执行顺序,并总结该过程.(教材:J ...
- drf Serializer使用
drf序列化 在前后端不分离的项目中,可以使用Django自带的forms组件进行数据验证,也可以使用Django自带的序列化组件对模型表数据进行序列化. 那么在前后端分离的项目中,drf也提供了数据 ...
- user.ini Operation not permitted
rm: cannot remove '/public/.user.ini': Operation not permitted chattr -i .user.ini rm -f .user.ini
- DOM0级事件误区-addEventListener
百度上很多篇文章讲解addEventListener DOM0级事件的时候讲解的都是覆盖 概念如下: 同一个元素的同一种事件只能绑定一个函数,否则后面的函数会覆盖之前的函数 其实不然,官方讲解:添加的 ...
- JUC---04Lock(二)ReentrantReadWriteLock
1.读写锁 分为读锁和写锁,多个读锁不互斥,读锁与写锁互斥,这是由jvm自己控制的,你只要上好相应的锁即可.如果你的代码只读数据,可以很多人同时读,但不能同时写,那就上读锁:如果你的代码修改数据,只能 ...