公司最近在做一个图片服务器,遇到一个需求,浏览器输入文件夹 需要显示文件夹内所有内容。
废话不多说,简单介绍一下。 平时我们需要设置web容器,如果是本地的tomcat 直接在service.xml设置各种参数 比如listings 设置。 默认是false。 用户浏览器输入查看图片可以看到,但是要是直接把图片名字去了,显示文件夹。那么web容器是阻止的。这个也是安全性的考虑。那么需要解决这个问题直接改tomcat服务配置就行。
但是现在主流的是springboot。 springboot 都是内置的web容器。这个时候可以在application.xml 里面去直接设置spring.tomcat 等等操作。但是有的属性是私有的,是不能直接设置的。这个时候我们需要实现TomcatServletWebServerFactory这个类。 具体代码直接贴下。 其他的私有参数可以在这里设置。
package com.thinking.file.transfer.station;
import org.apache.catalina.Context;
import org.apache.catalina.Wrapper;
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 CustomizationBean implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
@Override
public void customize(TomcatServletWebServerFactory factory) {
factory.addContextCustomizers(new TomcatContextCustomizer() {
@Override
public void customize(Context context) {
// TODO Auto-generated method stub
// context.setDocBase("/Users/xxx/opt/Akka");
Wrapper defServlet = (Wrapper) context.findChild("default");
defServlet.addInitParameter("listings", "true");
defServlet.addInitParameter("readOnly", "false");
defServlet.addMapping("/files/*");
}
});
}
}