zl程序教程

您现在的位置是:首页 >  后端

当前栏目

springboot 加入websocket后,ServerEndpointExporter配置不识别-解决

2023-09-14 09:06:39 时间

1.背景

springboot 加入websocket,需要配置ServerEndpointExporter的bean,发现没法识别

2.原因

springboot 内置了tomcat,内置 的tomcat与websocket不兼容,因此需要将 -start-web里的tomcat排除掉即可

3.解决

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

4.bug

tomcat排除掉后,会导致接口的文件传输对象【  MultipartFile  】没有tomcat封装方法,导致以前的简易操作文件方式用不了,只能通过流来处理

4.bug解决

我自己封装了个方法

//有websocket的项目使用这个办法
FileUtil.writeFile(vo.getFile().getInputStream(), aboPath);
 //写入文件
    public static boolean writeFile(InputStream fis, String tarFilePath) {
        try {
            FileOutputStream fos = new FileOutputStream(tarFilePath);
            byte[] b = new byte[1024];
            while ((fis.read(b)) != -1) {
                fos.write(b);// 写入数据
            }
            fis.close();
            fos.close();// 保存数据
        } catch (Exception e) {
            return false;
        }
        return true;
    }