Springboot上传文件时提示405问题及排坑过程
作者:zhangjp505 发布时间:2022-12-13 03:03:58
Springboot上传文件时提示405
问题描述:上传文件时请求不通,状态码返回405,如下图:
问题分析:405 Method Not Allowed,请求行中指定的请求方法不能被用于请求相应的资源。该响应必须返回一个Allow 头信息用以表示出当前资源能够接受的请求方法的列表。简单说就是请求方法不支持,这样就找到了一个解决方向。(以下分析了3种返回该错误的情况)
解决方案1
看下接口是否支持请求的方式,文件上传使用的POST方法,看下接口是否支持。后台日志如下:
解决方案2
发现接口确实支持POST请求,那么问题就不是这么明显了。因为该接口是用于文件上传,所以问题应该是在这里。由于Springboot默认的文件上传大小为1MB,自己再看发现文件大小超过了限制(上传的7.13MB),后台日志如下:
修改Springboot配置文件:
# 找到Springboot的application.properties配置文件,新增以下配置
spring.servlet.multipart.enabled=true #是否启用http上传处理
spring.servlet.multipart.max-request-size=10MB #最大请求文件的大小
spring.servlet.multipart.max-file-size=10MB #设置单个文件最大长度
解决方案3:修改文件大小配置后,发现还是报405,这就懵了,继续看后台日志:
明白了,发现后端使用@RequestParam(“file”) 注解标注的MultipartFile参数没有获取到文件,对比前端请求参数名发现不一致造成的。
问题解决。
Springboot使用过程中遇到的一些问题
仅记录个人遇到的一些问题及原因,和解决方案。
异常一
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: ContextPath must start with '/' and not end with '/'
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at com.pjx.Application.main(Application.java:15) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: ContextPath must start with '/' and not end with '/'
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:199) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:162) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
... 8 common frames omitted
Caused by: java.lang.IllegalArgumentException: ContextPath must start with '/' and not end with '/'
at org.springframework.boot.context.embedded.AbstractConfigurableEmbeddedServletContainer.checkContextPath(AbstractConfigurableEmbeddedServletContainer.java:132) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.context.embedded.AbstractConfigurableEmbeddedServletContainer.setContextPath(AbstractConfigurableEmbeddedServletContainer.java:120) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.autoconfigure.web.ServerProperties.customize(ServerProperties.java:198) ~[spring-boot-autoconfigure-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor.postProcessBeforeInitialization(EmbeddedServletContainerCustomizerBeanPostProcessor.java:73) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor.postProcessBeforeInitialization(EmbeddedServletContainerCustomizerBeanPostProcessor.java:59) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
... 16 common frames omitted
原因:配置项目名称配置错误
错误配置:
server:
context-path: admin
正确配置:
server:
context-path: /admin
异常二:Mysql连接报错
Could not create connection to database server
原因:MySql版本(8.0.28)和MySql驱动版本(5.1.45)不匹配,驱动版本换成8.0+就ok了。
使用:select version() from DUAL查询mysql版本。
异常三:整合Druid密码解密失败
异常栈:Caused by: com.mysql.cj.exceptions.CJException: Access denied for user 'root'@'localhost' (using password: YES)
分析:密码使用明文时,能正常连接数据库,查询到表数据。分析是密码没有成功解密。
pom添加的Druid依赖如下:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
Druid解密是在ConfigFilter中init中进行的,断点跟踪如下图,配置中的config.decrypt,config.decrypt.key是连在一起的,没有分开,所以decrypt=false,没有对密码进行解密操作
检查配置,数据库配置如下图
原因:connect-properties是druid-spring-boot-starter包下的配置参数,没有对properties进行解析处理,换成Druid报下的配置参数connection-properties。properties参数会进行按 ; 进行分隔处理,获取到具体的配置信息。
解决方案:换成connection-properties即可
来源:https://blog.csdn.net/yiluxiangqian7715/article/details/121702186


猜你喜欢
- 做Android开发两年的时间,技术稍稍有一些提升,刚好把自己实现的功能写出来,记录一下,如果能帮助到同行的其他人,我也算是做了件好事,哈哈
- 目录wait-notifyjoin方式ReentrantLockReentrantLock+ConditionSemaphore三个线程T1
- .NET 中的正则表达式是基于 Perl 5 的正则表达式。超时从 .NET Framework 4.5 开始,正则表达式支持在匹配操作中指
- Java注解的Excel导出依赖: <dependency> &
- 由于项目需求,需要将数据导出成Excel表格,并且可选择导出项,可下载。项目使用的Spring+Mybatis+SpringMVC框架,利用
- 一、实体类转换成XML将实体类转换成XML需要使用XmlSerializer类的Serialize方法,将实体类序列化public stat
- 这篇文章主要介绍了简单了解Java方法的定义和使用实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要
- 本文为大家分享了android实现图片橡皮擦和快速染色的具体代码,供大家参考,具体内容如下源码地址:Eraselmg1.染色 &n
- 背景数据之间两两趋势比较在数据分析应用中是非常常见的应用场景,如下所示:模拟考批次班级学生语文数学英语202302三年一班张小明130145
- 本文介绍了Maven构建自己的第一个Java后台的方法,分享给大家,具体如下:1.知识后顾关于如何运用Maven构建自己的第一个项目,上期我
- android客户端生成本地验证码主要用来限制用户随意按请求按钮,其实该示例也是来对自定义view的练练手而已,先给出效果图吧其中可定制:*
- 本文实例讲述了WinForm实现最小化到系统托盘方法。分享给大家供大家参考。具体分析如下:有个叫NotifyIcon的控件1、建个WinFo
- 现在android的每一个项目都会需要设置为全屏,现在介绍两种设置为全屏的方式。一、在配置文件中设置android:theme=”@andr
- 我们在使用editText控件的时候,会遇到这样的一问题,就是我在输入时候,当我选择让文字变粗时,我输入的文字就会变粗,当我去掉选择时,再输
- 如何配置 * step1: 自定义 * /** * 自定义 * */public class MyInterceptor implemen
- 多线程细节问题sleep方法和wait方法的异同点?相同点:让线程处于冻结状态.不同点:sleep必须指定时间 wait可以指定时间也可以不
- 概述在平时开发过程中经常会碰到需要使用圆角button的情况,一般也会包括很多其他小功能,比如要在里面添加img,设置不同的圆角大小等。针对
- 最近项目中使用了 MyBatis-Plus,点击看官方文档。使用一个新的框架,首先是验证框架的使用。 使用 MyBatis-Plu
- 二分法查找,顾名思义就是要将数据每次都分成两份然后再去找到你想要的数据,我们可以这样去想,二分法查找很类似与我们平时玩的猜价格游戏,当你报出
- 本文实例为大家分享了java实现顺时针打印矩阵的具体代码,供大家参考,具体内容如下题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每