• 团队代码


    2021年6月12日:

    工具类代码:

    package com.atguigu.crud.utils;

    import javax.websocket.*;
    import javax.websocket.server.PathParam;
    import javax.websocket.server.ServerEndpoint;
    import org.springframework.stereotype.Component;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;

    @ServerEndpoint("/websocket/{userno}")
    @Component
    public class websocket {
    private static ConcurrentHashMap<String, websocket> webSocketSet = new ConcurrentHashMap<String, websocket>();
    private static Map<String, List<String>> map = new HashMap<>();
    private static List<String> list = new ArrayList<>();
    private Session WebSocketsession;
    private String userno = "";

    @OnOpen
    public void onOpen(@PathParam(value = "userno") String param, Session WebSocketsession, EndpointConfig config)
    throws IOException {
    userno = param;
    this.WebSocketsession = WebSocketsession;
    websocket webSocketServer = webSocketSet.get(param);
    if (webSocketServer != null) { // 同样业务的连接已经在线,则把原来的挤下线。
    webSocketServer.WebSocketsession.getBasicRemote().sendText(param + "重复连接被挤下线了");
    webSocketServer.WebSocketsession.close();
    }
    webSocketSet.put(param, this);
    if (map.get(param) != null) {
    webSocketSet.get(param).sendMessage(param);
    }
    }

    @OnClose
    public void onClose() {
    if (!userno.equals("")) {
    webSocketSet.remove(userno);
    }
    }

    @OnMessage
    public void onMessage(String message) {
    if (message.split("[|]")[1].contains(",")) {
    sendAll(message.split("[|]")[1].split(","));
    } else {
    sendToUser(message);
    }
    }
    public void sendToUser(String message) {
    String sendUserno = message.split("[|]")[1];
    String sendMessage = message.split("[|]")[0];
    try {
    if (webSocketSet.get(sendUserno) != null) {
    webSocketSet.get(sendUserno).sendMessage(sendUserno);
    } else {
    if (map.get(sendUserno) == null) {
    map.put(sendUserno, list);
    map.get(sendUserno).add(sendMessage);
    } else {
    map.get(sendUserno).add(sendMessage);
    }
    }
    } catch (IOException e) {
    }
    }

    private void sendAll(String a[]) {
    for (String key : a) {
    try {
    if (webSocketSet.get(key) != null) {
    webSocketSet.get(key).sendMessage(key);
    } else {
    if (map.get(key) == null) {
    map.put(key, list);
    map.get(key).add(key);
    } else {
    map.get(key).add(key);
    }
    }
    } catch (IOException e) {
    }
    }
    }

    @OnError
    public void onError(Session session, Throwable error) {
    error.printStackTrace();
    }

    public void sendMessage(String message) throws IOException {
    this.WebSocketsession.getBasicRemote().sendText(message);
    }
    }

    package com.atguigu.crud.utils;

    import java.util.Random;

    public class ValidateCodeTool {

    public static String generateCode(){
    String str="0123456789";
    StringBuilder sb=new StringBuilder(5);
    for(int i=0;i<5;i++)
    {
    char ch=str.charAt(new Random().nextInt(str.length()));
    sb.append(ch);
    }
    return sb.toString();
    }
    }

    package com.atguigu.crud.utils;

    import sun.misc.BASE64Decoder;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.OutputStream;

    import org.springframework.stereotype.Component

    @Component
    public class UploadImgTool {

    public static boolean generateImg(String imgData, String saveDirectory, String fileName) {
    File directroy = new File(saveDirectory);
    if (directroy.exists() == false) {
    directroy.mkdir();
    }
    if (imgData == null || imgData.length() == 0) {
    return false;
    }

    BASE64Decoder decoder = new BASE64Decoder();
    try {
    byte[] b = decoder.decodeBuffer(imgData);
    for (int i = 0; i < b.length; ++i) {
    if (b[i] < 0) {
    b[i] += 256;
    }
    }
    File target = new File(saveDirectory, fileName);
    OutputStream out = new FileOutputStream(target);
    out.write(b);
    out.flush();
    out.close();
    return true;
    } catch (Exception e) {
    return false;
    }
    }
    }

  • 相关阅读:
    工厂模式
    将博客搬至CSDN
    网络安全-跨站脚本攻击XSS(Cross-Site Scripting)
    Linux 权限
    git常用的语句
    git代码提交与克隆
    git学习
    Mybatis常见问题
    关于集合常见的问题
    远程连接(加密验证问题解决)
  • 原文地址:https://www.cnblogs.com/yitiaokuailedexiaojingyu/p/14878077.html
Copyright © 2020-2023  润新知