Java 通过Spring服务提供文件

Java 通过Spring服务提供文件,java,spring,spring-mvc,Java,Spring,Spring Mvc,我有一个小型服务器,在src/main/resources中有两个.exe/.zip文件 我想通过我的服务提供这些文件 我尝试的是: @Configuration @EnableWebMvc public class Context extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { reg

我有一个小型服务器,在src/main/resources中有两个.exe/.zip文件

我想通过我的服务提供这些文件

我尝试的是:

@Configuration
@EnableWebMvc
public class Context extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
         registry.addResourceHandler("resources/**")
                 .addResourceLocation("classpath:/");

    }
}
这很好,但问题是我无法设置文件的内容类型。例如,当我访问.zip文件时,二进制代码在浏览器中呈现为文本。有没有办法根据文件扩展名设置内容类型


还有一种方法可以在不创建控制器的情况下设置简单的转发(公开包含文件的特定文件夹)。除非控制器是个更好的主意?

将下面的内容添加到您的web.xml

<mime-mapping>
   <extension>zip</extension>
    <mime-type>application/zip</mime-type>
</mime-mapping>
<mime-mapping>
    <extension>exe</extension>
    <mime-type>application/x-msdownload</mime-type>
</mime-mapping>

拉链
应用程序/zip
exe
应用程序/x-msdownload