我正在面臨Spring mvc下載包含名稱中包含空格的文件的問題。 我想要做的是使用內置的Spring MVC資源解析器從遠程主機下載文件。彈簧mvc - 文件名下載空白時出錯
我使用Spring安全以及Spring MVC的,但測試沒有具體的模式應用任何安全
春季安全配置:
<http>
....
<intercept-url pattern="/test download/**" access="permitAll" requires-channel="https" />
...
</http>
然後請求到達MVC調度器,其中配置資源解析器等:
Spr荷蘭國際集團MVC配置:
<mvc:resources mapping="/test download/**" location="http://localhost:10089/test/" />
的配置工作只是罰款不具有名稱空間中的文件。在Spring模塊上啓用調試日誌我發現這兩個請求(在文件名中有或沒有空格)都以完全相同的方式處理。 解決這兩個調用時,在Spring調度程序servlet的日誌之下。
第一次測試路徑:/測試下載/ file.txt的
... after security filters ..
DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'mvc-dispatcher' processing GET request for [/portal/test%20download/file.txt]
DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /test download/file.txt
DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/test download/file.txt]
DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /test download/file.txt
DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/test download/file.txt]
DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Matching patterns for request [/test download/file.txt] are [/test download/**]
DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - URI Template variables for request [/test download/file.txt] are {}
DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapping [/test download/file.txt] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[URL [http://localhost:10089/test/]], resolvers=[[email protected]6eb843]]] and 1 interceptor
DEBUG org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/portal/test%20download/file.txt] is: -1
DEBUG org.springframework.security.web.context.HttpSessionSecurityContextRepository - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
DEBUG org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'mvc-dispatcher': assuming HandlerAdapter completed request handling
DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request
DEBUG org.springframework.security.web.access.ExceptionTranslationFilter - Chain processed normally
DEBUG org.springframework.security.web.context.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
結果:
Content of the file is shown in the browser as expected
第二測試路徑:/測試下載/測試file.txt的
... after security filters ..
DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'mvc-dispatcher' processing GET request for [/portal/test%20download/test%20file.txt]
DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /test download/test file.txt
DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/test download/test file.txt]
DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /test download/test file.txt
DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/test download/test file.txt]
DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Matching patterns for request [/test download/test file.txt] are [/test download/**]
DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - URI Template variables for request [/test download/test file.txt] are {}
DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapping [/test download/test file.txt] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[URL [http://localhost:10089/test/]], resolvers=[[email protected]6eb843]]] and 1 interceptor
DEBUG org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/portal/test%20download/test%20file.txt] is: -1
DEBUG org.springframework.security.web.context.HttpSessionSecurityContextRepository - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
DEBUG org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'mvc-dispatcher': assuming HandlerAdapter completed request handling
DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request
DEBUG org.springframework.security.web.access.ExceptionTranslationFilter - Chain processed normally
DEBUG org.springframework.security.web.context.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
結果:
Http 404 is returned
的2個文件是在主機的同一文件夾,我都使用用於文件的本地服務器,並使用相同的結果的遠程服務器測試。
我試圖解決我的需求,只是使用Spring配置,因爲我需要的基本上類似於代理,但應用了安全規則。
Spring MVC的版本:4.1.6.RELEASE
Spring Security的版本:3.2.5.RELEASE
任何想法,爲什麼發生這種情況?
UPDATE
弗雷德裏克提示後,我配置了我使用測試遠程主機管理UTF-8 tomcat的,我發現,當它處理從春天來的請求,則返回505。
簡單的調用從瀏覽器到遠程主機:
「GET /test/test%20file.txt HTTP/1.1」,通過春收到200 15
電話:
「HEAD/test/test file.txt HTTP/1.1」505 -
我甚至在配置連接器之前嘗試在名稱中創建帶有%20的文件,並且它不起作用,因此可能甚至在編碼工作之前。
您如何在瀏覽器中顯示網址?也許你可以嘗試編碼URL,然後在瀏覽器中顯示它:'java.net.URLEncoder.encode(url,「UTF-8」)' –
這裏的問題似乎是在URL從Spring Security層解碼之後不被資源處理程序重新編碼。調試默認資源處理程序(在您發佈的類中使用),不會重新編碼空格。在調試中,我用%20覆蓋了運行時的值(感謝eclipse),並且發現文件沒有問題。所以我認爲現在唯一的解決方案是擴展默認的資源處理程序添加到解析器「特定路徑」/ test download/**的「重新編碼」步驟。 – Stefano