Spring:在上下文根之外提供静态资源

发布于 2024-10-27 09:21:26 字数 437 浏览 1 评论 0原文

在网络应用程序中,我需要提供位于应用程序上下文目录之外的静态内容(图像)。整个应用程序架构要求我使用 Tomcat 来执行此操作。我认为我可以受益于 Spring 的 来配置应用程序 URL 和目录内容之间的映射。但据我所知,它的 mapping 属性仅处理上下文相关或类路径映射。因此,我想使用的 :

<mvc:resources location="/images/**" mapping="/absolute/path/to/image/dir"/>

不起作用。因为我宁愿避免编写一个简单的文件传输 servlet,所以如果有人能给我一些关于现有的基于 Spring 的解决方案/解决方法的指示,我会很高兴。

非常感谢。

荷马

in a web app, I need to serve static contents (images) located outside the application context directory. The overall application architecture requires me to use Tomcat to perform this. I thought I could benefit from Spring's <mvc:resources> to configure a mapping between application URLs and directory contents. But AFAIK it's mapping attribute only handles context relative, or classpath mappings. Hence, what I'd like to use :

<mvc:resources location="/images/**" mapping="/absolute/path/to/image/dir"/>

doesn't work. As I'd rather avoid writing a simple file transfer servlet, I'd be glad if anyone could give me some pointers on existing Spring based solutions/workarounds.

Many thanks.

Homer

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

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

发布评论

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

评论(2

情痴 2024-11-03 09:21:26

可以从外部提供资源,需要使用常用的Spring 资源路径语法

<mvc:resources mapping="/images/**" location="file:/absolute/path/to/image/dir/"/> 

<mvc:resources> can serve resources from the outside, you need to use the usual Spring resource path syntax:

<mvc:resources mapping="/images/**" location="file:/absolute/path/to/image/dir/"/> 
独自←快乐 2024-11-03 09:21:26

还有一个更简单的更正,

代码应该是

你注意到差异了吗?
您需要将“/”放在绝对路径的末尾。

或者你可以使用java配置

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    String rootPath = System.getProperty("user.home");
    String imagePath = "file:"+rootPath + File.separator + "tmpFiles/";
    System.out.println(imagePath);
    registry.addResourceHandler("/resources/**").addResourceLocations("resources/");
    registry.addResourceHandler("/tmpFiles/**").addResourceLocations(imagePath);
}

它对我有用。

There is one more simple correction

the code should be

<mvc:resources mapping="/images/**" location="file:/absolute/path/to/image/dir/"/>

Did you notice the difference ?
You need to put '/' at the end of the absolute path.

or you can use the java configuration

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    String rootPath = System.getProperty("user.home");
    String imagePath = "file:"+rootPath + File.separator + "tmpFiles/";
    System.out.println(imagePath);
    registry.addResourceHandler("/resources/**").addResourceLocations("resources/");
    registry.addResourceHandler("/tmpFiles/**").addResourceLocations(imagePath);
}

Its working for me.

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