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默认允许请求长度 ...
随机推荐
- day48 Pyhton 数据库Mysql 05
一内容回顾 insert insert into 表名 (字段名) values (值) insert into 表名 values (有多少个字段写多少个值) insert into 表名 val ...
- GCC编译选项笔记
警告选项 -Wall:开启大多数的警告信息 -Wextra:开启额外的警告信息,比如参数未使用警告(-Wunused-parameter) -Werror:将警告当作错误,中断编译 优化选项 -O,- ...
- 【图论】HDU 5961 传递
题目内容 题目链接 我们称一个有向图G是传递的当且仅当对任意三个不同的顶点a,若G中有 一条边从a到b且有一条边从b到c ,则G中同样有一条边从a到c. 我们称图G是一个竞赛图,当且仅当它是一个有向图 ...
- PHP之Trait详解 转
php从以前到现在一直都是单继承的语言,无法同时从两个基类中继承属性和方法,为了解决这个问题,php出了Trait这个特性 用法:通过在类中使用use 关键字,声明要组合的Trait名称,具体的Tra ...
- Pyrhon-OS库
import os 导入OS模块 print(os.getcwd()) #获取当前路径 print(os.listdir()) #当前路径下文件信息 关于路径 Windows 下的路径采用 (\) 在 ...
- Martyr2项目实现——Number部分问题求解(3) Prime Factorization
Martyr2项目实现--Number部分问题求解(3) Prime Factorization 质因子分解 问题描述: Prime Factorization – Have the user ent ...
- Linux中创建自己的欢迎登陆界面
/etc 在Linux中相当于Windows的注册表 修改其中文件可以影响整个Linux系统 MOTD motd:message of the day /etc/motd /etc/motd文件作用是 ...
- update不能直接使用select的返回结果
update不能直接使用select的返回结果,这是会报错的,这是SQL的语法规定的,若想在update中与select结合使用,sql需要这样写: 1.其中field1,field2为表中的字段名 ...
- 关于 Deployer 部署结构
Deployer 部署完成后,在服务器上的结构会是这样子: drwxr-sr-x 5 deployer www-data 4096 Jun 14 09:53 ./ drwxr-sr-x 6 deplo ...
- vue学习第一部
目录 基础操作 vue基础使用 步骤 vue的框架思想(mvvm) 显示数据 vue 常用指令 属性操作 事件绑定 操作样式 条件渲染指令 列表渲染指令 vue对象提供的属性功能 过滤器 计算和侦听属 ...