欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【错误处理】上传文件出错returned a response status of 405 Method Not Allowed,400

程序员文章站 2022-04-14 13:44:32
...

异常

Type 异常报告

消息 Request processing failed; nested exception is com.sun.jersey.api.client.UniformInterfaceException: PUT http://localhost:9090/spring_file_server_war_exploded/upload/a19dfbef613d4127926c0ccd82881376_SnapShot_2020-03-17_15-51-14.png returned a response status of 405 Method Not Allowed

描述 服务器遇到一个意外的情况,阻止它完成请求。

Exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.sun.jersey.api.client.UniformInterfaceException: PUT http://localhost:9090/spring_day2_file_server_war_exploded/upload/a19dfbef613d4127926c0ccd82881376_SnapShot_2020-03-17_15-51-14.png returned a response status of 405 Method Not Allowed
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:986)
	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:881)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
Root Cause

com.sun.jersey.api.client.UniformInterfaceException: PUT http://localhost:9090/spring_day2_file_server_war_exploded/upload/a19dfbef613d4127926c0ccd82881376_SnapShot_2020-03-17_15-51-14.png returned a response status of 405 Method Not Allowed
	com.sun.jersey.api.client.WebResource.voidHandle(WebResource.java:709)
	com.sun.jersey.api.client.WebResource.put(WebResource.java:208)
	com.qichun.controller.UserController.testUpload3(UserController.java:154)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	java.lang.reflect.Method.invoke(Method.java:498)
	org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
	org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
	org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:871)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:777)
	org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:881)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
):注意 主要问题的全部 stack 信息可以在 server logs 里查看

环境

  • tomcat8
  • win10
  • idea部署

问题描述

向服务器上传文件报错,状态码405,400。
可能出现问题:

  • tomcat不允许写入请求a response status of 405 Method Not Allowed;
  • 上传的文件出错,常见的是文件名无法解析中文
  • 上传的地址错误,拼错URL

解决方法

  1. 修改tomcat的web.xml配置文件,允许http请求方法写入文件
    找到tomcat安装目录,修改conf文件夹下的web.xml文件。
    【错误处理】上传文件出错returned a response status of 405 Method Not Allowed,400
<init-param>
            <param-name>readonly</param-name>
            <param-value>false</param-value>
</init-param>

参数释义:
如果运行环境readonly(只读),则tomcat会拒绝所有的HTTP的修改方法(如put,delete),默认为true。

  <!--   readonly            Is this context "read only", so HTTP           -->
  <!--                       commands like PUT and DELETE are               -->
  <!--                       rejected?  [true]                              -->
  1. 上传文件名引起的错误可以对文件名进行转码即可解决,可以先用非中文文件名的文件试一试。

  2. 通过输出语句可以检测上传URL是否出错,纠错后再次尝试。