2011-11-07 106 views
4

我已经存储在一个文件夹(例如C:\ test \ tes.png)中的媒体(图片和电影),我试图访问与像这样的网址的图片:http://localhost:8080/app/picture/test.png 。 要做到这一点,我已经使用的资源标签(弹簧3)如下:春天mvc:资源访问外部文件夹

<mvc:resources mapping="/picture/**" location="file:/test" /> 

当我尝试访问,我有没有更多的细节错误。

请求资源未找到

我在日志:

2011-11-07 20:48:55241 [HTTP-8080-2] DEBUG org.springframework.web .servlet.DispatcherServlet - 名为'Family'的DispatcherServlet处理[/Family/photos/testImage2.png]的GET请求 2011-11-07 20:48:55,241 [http-8080-2] DEBUG org.springframework.web。 servlet.handler.SimpleUrlHandlerMapping - 请求[/photos/testImage2.png]的匹配模式是[/ **] 2011-11-07 20:48:55,241 [http-8080-2] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - 请求的URI模板变量[/photos/testImage2.png]为{} 2011- 11-07 20:48:55,242 [http-8080-2] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - 使用handler [org.springframework.web.servlet。将映射[/photos/testImage2.png]映射到HandlerExecutionChain。 [email protected]]和4个拦截器 2011-11-07 20:48:55,242 [http-8080-2] DEBUG org.springframework.web.servlet.DispatcherServlet - [/ Family/photos/testImage2的Last-Modified值.png]:-1 2011-11-07 20:48:55,242 [http-8080-2] DEBUG org.springframework.web.servlet.DispatcherServlet - Null ModelAndView返回名为'Family'的DispatcherServlet:假设HandlerAdapter已完成请求处理 2011-11-07 20:48:55 ,242 [HTTP-8080-2] DEBUG org.springframework.web.servlet.DispatcherServlet - 成功完成请求

我肯定不是全部明白...

另一个问题是:我不知道这是一个好方法。访问外部文件夹上媒体的其他解决方案是什么?

在此先感谢!

+0

你可以在你的webapp中拥有一个媒体文件夹的快捷方式吗? –

+0

查看Spring Resources servlet。它可以提供你想要的功能。 – DwB

回答

3

第一个问题:“映射”

我不是100%肯定,但我猜想,有一个最终的/缺失的位置。 将其更改为:

<mvc:resources mapping="/picture/**" location="file:/test/" /> 

另一个问题是:我不知道这是好办法。访问外部文件夹上媒体的其他解决方案是什么?

在我的拙见认为,让网站用户完全读取文件夹的权限是非常糟糕的做法。注意访问不仅限于该文件夹,而且用户还可以访问所有子文件夹。

*即使您决定忽略此警告,那么您必须测试会发生什么,如果一些使用调用 http://localhost:8080/app/picture/../someFile。**我不知道会发生什么,但**使120%确保没有人可以访问 picture文件夹之外的任何文件! - 我已经看了一下Spring的实现,并且它已经处理了这个问题。* 自从Spring 3.2.12,4.0.8,4.1.2 Resource Handler确保你不访问一个文件夹在指定的资源文件夹之外。 (SPR-12354: Directory traversal with static resource handling (CVE-2014-3625)

+1

感谢ralph提供的建议!您对此练习无疑是正确的,因此我使用了MediaServlet来检索它们。有关图像示例,请参见[link](http://balusc.blogspot.com/2007/04/imageservlet.html)。伟大的工作! – user1034407

+0

感谢您的链接!保存了我的夜晚! hauhaahua –