shichongfu
2023-10-08 6f83c8971714e2384df45706e364ed45a0cd1183
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.product.device.data.gather.service;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.nio.charset.Charset;
 
import com.product.core.dao.BaseDao;
import com.product.core.spring.context.SpringMVCContextHolder;
 
import org.mozilla.universalchardet.UniversalDetector;
 
 
public class DeviceDataProcessThread extends Thread {
    Charset utf8Charset = Charset.forName("UTF-8");
    Charset gbkCharset = Charset.forName("GBK");
    Charset gbCharset = Charset.forName("GB2312");
    Charset ISOCharset = Charset.forName("ISO-8859-1");
    Charset US = Charset.forName("US-ASCII");
    
    public BaseDao baseDao;
    
    Socket socket = null;
    //每启动一个线程,连接对应的Socket
 
    public DeviceDataProcessThread(Socket socket,BaseDao dao) {
        this.socket = socket;
        this.baseDao=dao;
    }
    
    //启动线程,即响应客户请求
    public void run() { 
        BufferedReader reader=null;
        
        BufferedWriter writer=null; 
        try {
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));//读取客户端消息  
            writer=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));//向客户端写消息
            String line="";
            while((line=reader.readLine()) !=null ){
//                SpringMVCContextHolder.getSystemLogger().info("=============="+encode(line));
                SpringMVCContextHolder.getSystemLogger().info("=====字符编码:"+getEncoding(line));
                SpringMVCContextHolder.getSystemLogger().info("=====收到来自客户端发送的消息是:" + line);
//                SpringMVCContextHolder.getSystemLogger().info("=====收到来自客户端的发送的消息是1111:" + new String(line.getBytes(utf8Charset)));
//                SpringMVCContextHolder.getSystemLogger().info("=====收到来自客户端的发送的消息是1:" + new String(line.getBytes(US),gbkCharset));
//                SpringMVCContextHolder.getSystemLogger().info("=====收到来自客户端的发送的消息是12:" + new String(line.getBytes(US),utf8Charset));
//                String lineGBK = new String(line.getBytes(ISOCharset), gbkCharset); 
//                SpringMVCContextHolder.getSystemLogger().info("=====收到来自客户端的发送的消息是2:" + lineGBK);
//                String lineUTF8 = new String(line.getBytes(ISOCharset), utf8Charset); 
//                SpringMVCContextHolder.getSystemLogger().info("=====收到来自客户端的发送的消息是3:" + lineUTF8);
//                String lineiso = new String(line.getBytes(utf8Charset), ISOCharset); 
//                SpringMVCContextHolder.getSystemLogger().info("=====收到来自客户端的发送的消息是4:" + lineiso);
//                String lineug = new String(line.getBytes(utf8Charset), gbkCharset); 
//                SpringMVCContextHolder.getSystemLogger().info("=====收到来自客户端的发送的消息是5:" + lineug);
//                
//                String linegi = new String(line.getBytes(gbkCharset), ISOCharset); 
//                SpringMVCContextHolder.getSystemLogger().info("=====收到来自客户端的发送的消息是6:" + linegi);
//                String linegu = new String(line.getBytes(gbkCharset), utf8Charset); 
//                SpringMVCContextHolder.getSystemLogger().info("=====收到来自客户端的发送的消息是7:" + linegu);
            } 
            
          writer.write("服务器已收到:"+line+"\n");  
          writer.flush(); 
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                if(reader!=null) {
                    reader.close();
                }
                if(writer!=null) {
                    writer.close();
                }
//                if(socket!=null) {
//                    socket.close();
//                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    }
    
    public static  String encode(String str) {
//        String testString = "这是一个测试字符串";
        byte[] testData = str.getBytes();
 
        // 初始化 CharsetDetector
        UniversalDetector detector = new UniversalDetector(null);
 
        // 将数据填充到 CharsetDetector
        detector.handleData(testData, 0, testData.length);
 
        // 完成数据填充
        detector.dataEnd();
 
        // 获取检测出来的字符集
        String encoding = detector.getDetectedCharset();
 
        if (encoding != null) {
            System.out.println("编码格式为:" + encoding);
        } else {
            System.out.println("未能检测出编码格式。");
        }
 
        // 释放资源
        detector.reset();
        return encoding;
    }
    
    
    public static String getEncoding(String str) {
        String encode = "GB2312";
        try {
            if (isEncoding(str, encode)) { // 判断是不是GB2312
                return encode;
            }
        } catch (Exception exception) {
            
        }
        encode = "ISO-8859-1";
        try {
            if (isEncoding(str, encode)) { // 判断是不是ISO-8859-1
                return encode;
            }
        } catch (Exception exception1) {
        }
        encode = "UTF-8";
        try {
            if (isEncoding(str, encode)) { // 判断是不是UTF-8
                return encode;
            }
        } catch (Exception exception2) {
        }
        encode = "GBK";
        try {
            if (isEncoding(str, encode)) { // 判断是不是GBK
                return encode;
            }
        } catch (Exception exception3) {
        }
        return "如果都不是,说明输入的内容不属于常见的编码格式"; // 如果都不是,说明输入的内容不属于常见的编码格式。
    }
 
    public static boolean isEncoding(String str, String encode) {
        try {
            if (str.equals(new String(str.getBytes(), encode))) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
}