春季启动应用程序的嵌入式Tomcat目录列表 [英] Embedded Tomcat directory listing for spring-boot application

查看:136
本文介绍了春季启动应用程序的嵌入式Tomcat目录列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有嵌入式Tomcat的spring boot应用程序. 我想公开一些图片文件和通过tomcat目录列表从其他位置复制文件夹.所以我在名为

I have a spring boot application with embedded Tomcat. I wanted to expose some images files & folders from a different location via tomcat directory listing. So I added the below in my configuration file called

public class AppConfig extends WebMvcConfigurerAdapter

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**").addResourceLocations("file:///xxx/yyy/images/");
    }
}

如果知道名称,我现在可以访问单个图像.

I can now access individual image(s), if I know the name.

示例:localhost:8080/images/file.jpg.

Example: localhost:8080/images/file.jpg.

但是由于默认情况下目录列表为假,所以我无法通过"localhost:8080/images/"访问图像列表以了解所有可用图像.

But since the directory listing is false by default, I can't access the images listing through "localhost:8080/images/" to know the all the available images.

我尝试了以下选项来添加列表,但是没有用.

I tried the below option to add the listings as well, but did not work.

public class MyApplication implements ServletContextInitializer{

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        servletContext.setInitParameter("listings", "true");
    }
}

推荐答案

已针对Spring 2.1更新

Updated for Spring 2.1

import org.apache.catalina.Context;
import org.apache.catalina.Wrapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;

@Component
public class MyTomcatWebServerCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory>  {

    @Value("${tomcat.file.base}")  // C:\\some\\parent\\child
    String tomcatBaseDir;

    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        // customize the factory here
        TomcatContextCustomizer tomcatContextCustomizer = new TomcatContextCustomizer() {
            @Override
            public void customize(Context context) {
                String parentFolder = tomcatBaseDir.substring(0,tomcatBaseDir.lastIndexOf("\\"));
                String childFolder = tomcatBaseDir.substring(tomcatBaseDir.lastIndexOf("\\") + 1);
                context.setDocBase(parentFolder);
                Wrapper defServlet = (Wrapper) context.findChild("default");
                defServlet.addInitParameter("listings", "true");
                defServlet.addInitParameter("readOnly", "false");
                defServlet.addMapping("/"+ childFolder + "/*");
            }
        };
        factory.addContextCustomizers(tomcatContextCustomizer);

    }
}

这篇关于春季启动应用程序的嵌入式Tomcat目录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆