默认
发表评论 0
想开发IM:买成品怕坑?租第3方怕贵?找开源自已撸?尽量别走弯路了... 找站长给点建议
问题工程
阅读(9625) | 评论(0 收藏 淘帖
https://gitee.com/hbh820326/linjp3
这是我的远程仓库已经提交了。以下是文件上传和下载服务,集成服务端就是把SDK放在本地仓库,用mave进行引用,
然后在工程启动类中new 出一个IM的启动类进行启动,在IM的启动类的构造器中写好启动语句。
/**********************这是文件上传下载服务**********************************************

@RestController
public class FileController {
    private static final Logger log = LoggerFactory.getLogger(FileController.class);

    // 单个文件上传
    @RequestMapping(value = "/upload")
    public String upload(@RequestParam("file") MultipartFile file) {    //注意参数
        try {
            File parentFile = new File(new ClassPathResource("").getFile(), "upload");
            if (!parentFile.exists()) {
                parentFile.mkdir();
            }
            File newFile = new File(parentFile, file.getOriginalFilename());
            file.transferTo(newFile);// 文件写入
            return "上传成功" + newFile.getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "上传失败";
    }

    // 多个文件一起上传
    @PostMapping("/batch")
    public String handleFileUpload(HttpServletRequest request) {   //注意参数
        try {
            File parentFile = new File(new ClassPathResource("").getFile(), "upload");
            if (!parentFile.exists()) {
                parentFile.mkdir();//第一次创建upload文件夹
            }
            List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
            BufferedOutputStream stream = null;
            for (MultipartFile mf : files) {
                stream = new BufferedOutputStream(new FileOutputStream(new File(parentFile, mf.getOriginalFilename())));//设置文件路径及名字
                stream.write(mf.getBytes());// 写入
                stream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "上传成功";
    }

    @GetMapping("/download")
    public String downloadFile(HttpServletRequest request, HttpServletResponse response) {
        String fileName = request.getParameter("filename");// 文件名
        if (fileName != null) {
            BufferedInputStream bis =null;
            try {
                File file = new File(new ClassPathResource("upload").getFile(), fileName);
                if (file.exists()) {
                    response.setContentType("application/force-download");// 设置强制下载不打开
                    response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
                    byte[] buffer = new byte[1024];
                    bis = new BufferedInputStream( new FileInputStream(file));
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    return "下载成功";
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    bis.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return "下载失败";
    }
}
//***************************************这是工程启动类**********************************************



@RestController
@SpringBootApplication(scanBasePackages = "com.linjp")
@EnableScheduling   //启用quartz功能
public class ApiApplication {
    @GetMapping("/test")
    public String test(){
        String s="成功";

        return s;
    }


    public static void main(String[] args) {
      SpringApplication.run(ApiApplication.class);


        try {
            new ServerLauncherImpl();//启动IM
        } catch (Exception e) {
            e.printStackTrace();
        }

//调线程池启动一个任务
//        TaskManager taskManager =applicationContext.getBean(TaskManager.class);
     // TaskParam taskParam=new TaskParam(LogTaskListener.class,3245l,"test");
//        taskManager.pushTask(taskParam);
    }

}


/******************************************这是IM启动类********************************
public class ServerLauncherImpl extends ServerLauncher {
   /**
    * 静态类方法:进行一些全局配置设置。
    */
   static
   {
      // 设置MobileIMSDK服务端的UDP网络监听端口
      GatewayUDP.PORT       = 7901;
      // 设置MobileIMSDK服务端的TCP网络监听端口
      GatewayTCP.PORT       = 8901;
      // 设置MobileIMSDK服务端的WebSocket网络监听端口
      GatewayWebsocket.PORT = 3000;

      // 设置MobileIMSDK服务端仅支持UDP协议
//    ServerLauncher.supportedGateways = Gateway.SOCKET_TYPE_UDP;
      // 设置MobileIMSDK服务端仅支持TCP协议
//    ServerLauncher.supportedGateways = Gateway.SOCKET_TYPE_TCP;
      // 设置MobileIMSDK服务端仅支持WebSocket协议
//    ServerLauncher.supportedGateways = Gateway.SOCKET_TYPE_WEBSOCKET;
      // 设置MobileIMSDK服务端同时支持UDPTCPWebSocket三种协议
      ServerLauncher.supportedGateways = Gateway.SOCKET_TYPE_UDP | Gateway.SOCKET_TYPE_TCP | Gateway.SOCKET_TYPE_WEBSOCKET;

      // /Demog日志的输出
      QoS4SendDaemonS2C.getInstance().setDebugable(true);
      QoS4ReciveDaemonC2S.getInstance().setDebugable(true);

      // 与客户端协商一致的心跳频率模式设置
//    ServerToolKits.setSenseModeUDP(SenseModeUDP.MODE_15S);
      ServerToolKits.setSenseModeTCP(SenseModeTCP.MODE_5S);
      ServerToolKits.setSenseModeWebsocket(SenseModeWebsocket.MODE_5S);
//    ServerToolKits.setSenseModeWebsocket(SenseModeWebsocket.MODE_30S);

      // 关闭与Web端的消息互通桥接器(其实SDK中默认就是false
      ServerLauncher.bridgeEnabled = false;
      // TODO 跨服桥接器MQURI(本参数只在ServerLauncher.bridgeEnabledtrue时有意义)
//    BridgeProcessor.IMMQ_URI = "amqp://js:19844713@192.168.0.190";

      // 设置最大TCP帧内容长度(不设置则默认最大是 6 * 1024字节)
//    GatewayTCP.TCP_FRAME_MAX_BODY_LENGTH = 60 * 1024;
   }

   /**
    * 实例构造方法。
    *
    * @throws IOException
    */
   public ServerLauncherImpl() throws Exception {
      startup();
      // 加一个钩子,确保在JVM退出时释放netty的资源
      Runtime.getRuntime().addShutdownHook(new Thread() {
         @Override
         public void run() {
            shutdown();
         }
      });
   }

    /**
     * 初始化消息处理事件监听者.
     */
   @Override
    protected void initListeners()
    {
       // ** 设置各种回调事件处理实现类
      this.setServerEventListener(new ServerEventListenerImpl());
      this.setServerMessageQoSEventListener(new MessageQoSEventS2CListnerImpl());
    }




    public static void main(String[] args) throws Exception
    {
       // 实例化后记得startup哦,单独startup()的目的是让调用者可以延迟决定何时真正启动IM服务
       //final ServerLauncherImpl sli = new ServerLauncherImpl();

       // 启动MobileIMSDK服务端的Demo

    }
}

即时通讯网 - 即时通讯开发者社区! 来源: - 即时通讯开发者社区!

上一篇:[学习资料] 稀缺的音视频资料下一篇:后台clean了下项目,再打war包里面缺少很多东西
推荐方案
打赏楼主 ×
使用微信打赏! 使用支付宝打赏!

返回顶部