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;
|
}
|
|
}
|