文件位置:經過文件上傳後保存在本地的一個upload_test文件夾中
配置WebConfig
實現WebMvcConfigurer類
@Configuration
public class WebConfig implements WebMvcConfigurer {}
重寫addResourceHandlers方法
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 配置訪問前綴
registry.addResourceHandler("/home/hobo/upload_test/**")
//配置文件真實路徑
.addResourceLocations("file:/home/hobo/upload_test/");
}
效果圖
如果用security的同學加載顯示401 權限認證失敗的話,在SecurityConfig 現將文件路徑配置爲公開
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
//token的驗證方式不需要開啓csrf的防護
.csrf().disable()
.exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint)
.and()
//設置無狀態的連接,即不創建session
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests()
// .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
// 配置靜態路徑
.antMatchers("/home/hobo/upload_test/**").permitAll()
.anyRequest().authenticated();
}