Java 如何拦截特定文件类型的请求?

Java 如何拦截特定文件类型的请求?,java,spring,spring-mvc,spring-3,Java,Spring,Spring Mvc,Spring 3,我们的应用程序是基于Java/Spring3/SpringMVC/Hibernate的应用程序 我们有一些资源存储在服务器机器的不同位置。这些位置存储在数据库中。基本上,当web应用程序从uri(如//page/file.kml)请求文件时,当需要拦截此调用时,忽略请求的uri,查找文件的实际位置并将其作为响应返回 在我们的servlet context.xml中,我们有一些拦截器 <interceptors> <interceptor> <

我们的应用程序是基于Java/Spring3/SpringMVC/Hibernate的应用程序

我们有一些资源存储在服务器机器的不同位置。这些位置存储在数据库中。基本上,当web应用程序从uri(如
//page/file.kml
)请求文件时,当需要拦截此调用时,忽略请求的uri,查找文件的实际位置并将其作为响应返回

在我们的
servlet context.xml
中,我们有一些拦截器

<interceptors>
    <interceptor>
        <mapping path="/page/**" />
        <beans:bean class="com.ourapp.AuthenticationInterceptor" />
    </interceptor>
    <interceptor>
        <mapping path="/page/*.kml" />
        <beans:bean class="com.ourapp.KmlInterceptor" />
    </interceptor>
</interceptors>

第一次拦截是为了我们的身份验证,效果很好。基本上确保用户登录任何请求

第二个拦截器是我们设置的,用于尝试拦截来自geoXML3的对KML文件的请求。拦截机好像没有开火?(即KmlInterceptor.preHandle没有被调用?)

我们在那里做正确的映射吗


这是拦截特定文件类型的请求并返回从其他地方检索到的实际文件的方法吗?

事实上,我们不再尝试使用拦截器,而是使用普通的
@RequestMapping
注释

@RequestMapping(value = "/page/location/*.kml", method = RequestMethod.GET)
public void getKMLFile(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        // Getting the filename (ie splitting off the /page/location/ part)
        String uri = httpRequest.getRequestURI();
        String[] parts = uri.split("/");
        String alais = parts[parts.length - 1];

        // Our app specific code finding the file from the db location 
        Resource resource = resourceService.getResourceByAlias(alais);
        File file = resource.getAbsFile();

        // Putting the file onto the httpResponse 
        InputStream is = new FileInputStream(file);
        IOUtils.copy(is, httpResponse.getOutputStream());
        httpResponse.flushBuffer();

    } catch (IOException e) {
        throw new RuntimeException("IOError writing file to output stream");
    }
}

文件路径是
/pages/something.kml
,还是有更多子目录?如果是,可能尝试使用
/page/***.kml
就可以了。这就是问题所在,在/page/something.kml中没有文件。我们希望捕获对该uri的请求,在别处找到该文件,并在响应中返回它。ie从不让web应用程序在/page/something.kmlYep上查找文件,我明白这一点。我这样问是因为如果假文档位于
/pages/adirectory/file.kml
hmm不,它肯定是在请求/page/file.kml,那么表达式可能是错误的