使用springboot时,一般只是开发接口.不会集成springmvc的功能,此时要使用文件服务就不能像配置web.xml哪种简单了.此时我们就要用到WebMvcConfigurationSupport类了,此类和web.xml功能是一样的.
重写以下方法,可开启文件服务
/** * 启用文件服务 * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { String thisPath = null; //生成excel文件 File directory = new File("");// 参数为空 //要生成的文件路径 try { thisPath = directory.getCanonicalPath() + "/tmp/appLastSubmit/"; } catch (IOException e) { e.printStackTrace(); }
//将网络请求映射到本地路径 registry.addResourceHandler("/op/tmp/appLastSubmit/**").addResourceLocations("file:"+thisPath); }