天天看點

【錯誤處理】上傳檔案出錯returned a response status of 405 Method Not Allowed,400

異常

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是否出錯,糾錯後再次嘗試。