java 设置自定义文件目录,浏览器可以直接打开该目录下的文件

浏览器可以直接打开的文件有html、pdf、xml等,office文件浏览器不能直接打开,若想要在线预览office文件,可以参照我其他的博文,office文件转换为html的实现代码

重写WebMvcConfigurerAdapter类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Component
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {

@Override
 public void addResourceHandlers(ResourceHandlerRegistry registry) {
  registry.addResourceHandler("/**")
        .addResourceLocations("classpath:/META-INF/resources/")
        .addResourceLocations("classpath:/resources/")
        .addResourceLocations("classpath:/static/")
        .addResourceLocations("classpath:/public/");
        registry.addResourceHandler("/onlineFile/**").addResourceLocations("file:D:/onlinefile/");
        super.addResourceHandlers(registry);
    }
}

上面的D:/onlinefile/就是设置的浏览器可以直接访问的文件路径,其中/onlineFile拼接在url上,浏览器的url:http://localhost:8103/onlineFile/U76bpqN4TWFcwkqg.html,这样就可以直接访问了

猜你喜欢

转载自blog.csdn.net/m0_37615697/article/details/81076988