admin管理员组

文章数量:1530950

 

浏览器可以直接打开的文件有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,这样就可以直接访问了

 

注意:

今天项目直接打开这个url出问题了,js调用window.open直接打开文件会被浏览器拦截,需要客户端设置允许弹出框不被拦截,对于用户来说体验感不好,而且有些浏览器根本没有拦截提示,在网上查了一下解决方法,各位可参考以下链接:

https://blog.csdn/u011159417/article/details/53692536

https://blog.csdn/yypsober/article/details/79487217

最简单的解决方法应该是这种:

var tempwindow=window.open('_blank'); // 先打开页面
tempwindow.location='http://www.baidu'; // 后更改页面地址
 

本文标签: 文件可以直接自定义浏览器目录