Spring mvc:访问外部文件夹的资源

发布于 2024-12-14 07:20:51 字数 1687 浏览 2 评论 0原文

我已将媒体(图片和电影)存储在文件夹中(例如 C:\test\tes.png),并且我尝试使用以下网址访问图片: http://localhost:8080/app/picture/test.png。 为此,我使用了资源标签(spring 3),如下所示:

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

当我尝试访问时,出现错误,没有更多详细信息。

未找到请求的资源

我在日志中:

2011-11-07 20:48:55,241 [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] 调试 org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - 请求的匹配模式 [/photos/testImage2.png] 是 [/**] 2011-11-07 20:48:55,241 [http-8080-2] 调试 org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - 请求 [/photos/testImage2.png] 的 URI 模板变量为 {} 2011-11-07 20:48:55,242 [http-8080-2] 调试 org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - 使用处理程序 [org.springframework.web.SimpleUrlHandlerMapping] 将 [/photos/testImage2.png] 映射到 HandlerExecutionChain。 servlet.resource.DefaultServletHttpRequestHandler@3a779f5e] 和4 拦截机 2011-11-07 20:48:55,242 [http-8080-2] 调试 org.springframework.web.servlet.DispatcherServlet - [/Family/photos/testImage2.png] 的上次修改值为:-1 2011-11-07 20:48:55,242 [http-8080-2] 调试 org.springframework.web.servlet.DispatcherServlet - Null ModelAndView 返回到名称为“Family”的 DispatcherServlet:假设 HandlerAdapter 已完成请求处理 2011-11-07 20:48:55,242 [http-8080-2] 调试 org.springframework.web.servlet.DispatcherServlet - 成功完成请求

我当然不明白......

另一个问题:我不确定这是好的方法。访问外部文件夹上的媒体还有哪些其他解决方案?

提前致谢 !

I have stored medias (pictures and movies) in a folder (for example C:\test\tes.png) and I'm trying to access to pictures with an url like : http://localhost:8080/app/picture/test.png.
To do that, i have used resources tag (spring 3) as below :

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

When I try to access, I have an error with no more details.

Requested Resource Not Found

I have in logs :

2011-11-07 20:48:55,241 [http-8080-2] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'Family' processing GET request for [/Family/photos/testImage2.png]
2011-11-07 20:48:55,241 [http-8080-2] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Matching patterns for request [/photos/testImage2.png] are [/**]
2011-11-07 20:48:55,241 [http-8080-2] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - URI Template variables for request [/photos/testImage2.png] are {}
2011-11-07 20:48:55,242 [http-8080-2] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapping [/photos/testImage2.png] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@3a779f5e] and 4 interceptors
2011-11-07 20:48:55,242 [http-8080-2] DEBUG org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/Family/photos/testImage2.png] is: -1
2011-11-07 20:48:55,242 [http-8080-2] DEBUG org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'Family': assuming HandlerAdapter completed request handling
2011-11-07 20:48:55,242 [http-8080-2] DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request

I have certainly not all understand...

Another question : I'm not sure this is the good approach. What are others solutions to access to media on external folder ?

Thanks in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终难遇 2024-12-21 07:20:51

第一个问题:“映射”

我不是 100% 确定,但我猜想,该位置缺少最后一个 /
将其更改为:

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

另一个问题:我不确定这是一个好方法。访问外部文件夹上的媒体还有哪些其他解决方案?

以我的拙见,向网站用户授予对文件夹的完全读取权限是非常糟糕的做法。注意,访问不仅限于文件夹,用户还可以访问所有子文件夹。

*即使您决定忽略此警告,您也必须测试如果某些使用调用 http://localhost:8080/app/picture/../someFile 会发生什么情况。**我不知道会发生什么,但是 **120% 确保没有人可以访问 picture 文件夹之外的任何文件! -- 我查看了 spring 的实现,发现 spring 已经处理了这个问题。*从 Spring 3.2.12、4.0.8、4.1.2 开始,资源处理程序确保您不访问指定资源文件夹之外的文件夹。 (SPR-12354:使用静态资源处理进行目录遍历 (CVE-2014-3625)

First question: "the mapping"

I am not 100% sure, but I would guess, there is a final / missing for the location.
change it to:

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

Another question : I'm not sure this is the good approach. What are others solutions to access to media on external folder ?

In my humble opinion, it is very bad practice to give an web site user full read access to an folder. Attention the access is not only limited to the folder, but the user can also access all sub folders.

*And even if you decided to ignore this warning, then you must test what happen if some use invoke http://localhost:8080/app/picture/../someFile.** I don't know what would happen, but **make 120% sure that nobody can access any file outside the picture Folder! -- I have had a look into the spring implementation, and it seams that spring already handle this issue.*Since Spring 3.2.12, 4.0.8, 4.1.2 the Resource Handler make sure that you an not access an folder outside the specified resource folder. (SPR-12354: Directory traversal with static resource handling (CVE-2014-3625))

走野 2024-12-21 07:20:51

如果您的 .js/.css/.img 文件存储在驱动器 d:\\ext_resources 中,则添加 弹簧配置文件。 例如 mvc-dispatcher-servlet.xml.xml 文件...

<mvc:resources mapping="/ext/**" location="file:d://ext_resources/"/>

这意味着/ext 将指示文件夹d:\\ext_resources,其中包含 .html/.jsp/.ftl/.jsf 文件。

<html>
    <link rel="stylesheet" href="/ext/style.css">
    <img src="/ext/avatar.jpg" height="100" width="100">
</html>

您也可以在 ext_resources 文件夹中拥有子文件夹,例如 image 文件夹。
然后使用 URL 中的子文件夹。

<img src="/ext/images/avatar.jpg" height="100" width="100">

If your .js/.css/.img files are stored in drive d:\\ext_resources then add <mvc:resources> in spring configaration file. e.g. mvc-dispatcher-servlet.xml.xml file...

<mvc:resources mapping="/ext/**" location="file:d://ext_resources/"/>

This means /ext will indicate folder d:\\ext_resources, which contains .html/.jsp/.ftl/.jsf files.

<html>
    <link rel="stylesheet" href="/ext/style.css">
    <img src="/ext/avatar.jpg" height="100" width="100">
</html>

you can have sub-folder too in ext_resources folder like image folder.
Then use subfolder in URL.

<img src="/ext/images/avatar.jpg" height="100" width="100">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文