CommandClient.java
4.9 KB
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
package com.sy.socket;
import com.sy.model.GatherInfo;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
@Component
public class CommandClient {
private static final Logger logger = Logger.getLogger(CommandClient.class);
/**
* @Param GatherInfo 卡口采集信息
* @Param flag 是否放行信息
* */
public static void Client(GatherInfo info,String flag) {
String xmlBody = getXmlInfo(info,flag);
Socket socket =null;
OutputStream op = null;
try {
//ip+端口
socket = new Socket("10.50.7.10", 9003);
logger.info("socket通讯创建连接成功");
op = socket.getOutputStream();
//xml字节流
byte[]xBody =xmlBody.getBytes("GB2312");
//包头
byte[] head = new byte[4];
head[0]=(byte)0xE2;
head[1]=(byte)0x5C;
head[2]=(byte)0x4B;
head[3]=(byte)0x89;
//消息类型
byte[] mType = new byte[1];
mType[0] = (byte)0x22;
//场站号
byte[]station =info.getAreaid().getBytes("ASCII");
//通道号
byte[]aisle =info.getChnlno().getBytes("ASCII");
//进出标识
byte[]eType =info.getIetype().getBytes("ASCII");
//标识符
byte[] bwFlag = new byte[4];
bwFlag[0]=(byte)0x00;
bwFlag[1]=(byte)0x00;
bwFlag[2]=(byte)0x00;
bwFlag[3]=(byte)0x00;
//xml字节流长度
byte[]xmlLength = intToByte4(xBody.length);
//包尾
byte[]end = new byte[2];
end[0]=(byte)0xFF;
end[1]=(byte)0xFF;
System.out.println();
//总长 4为总长的length
byte [] packge = intToByte4((head.length+xBody.length+mType.length+station.length+aisle.length+eType
.length+bwFlag.length+xmlLength.length+end.length+4));
byte[]allByte = byteMergerAll(head,packge,mType,station,aisle,eType,bwFlag,xmlLength,xBody,end);
op.write(allByte);
op.flush();
op.close();
logger.info("发送完毕");
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
logger.info("创建连接失败"+e.getMessage());
} catch (IOException e) {
e.printStackTrace();
logger.info("文件发送失败"+e.getMessage());
}
}
/**
* @Param info 卡口采集信息
* @Param flag 放行标识
* @Result 生成放行报文
* */
private static String getXmlInfo(GatherInfo info,String message) {
String flag = null;
if ("直接放行".equals(message)){
flag = "00";
} else{
flag = "11";
}
StringBuffer buffer = new StringBuffer();
buffer.append("<COMMAND_INFO AREA_ID=\""+info.getAreaid()+"\" CHNL_NO=\""+info.getChnlno()+"\" I_E_TYPE=\""+info
.getIetype()+"\" SEQ_NO=\""+info.getSeqno()+"\">");
buffer.append("<CHECK_RESULT>"+flag+"000000000000000000</CHECK_RESULT><OP_HINT>"+message+"</OP_HINT>");
buffer.append("<SEAL>");
buffer.append("<ESEAL_ID>"+info.getEsealid()+"</ESEAL_ID>");
buffer.append("<SEAL_KEY>"+info.getSealkey()+"</SEAL_KEY>");
buffer.append("<OPEN_TIMES></OPEN_TIMES>");
buffer.append("<ESEAL_IC_NO></ESEAL_IC_NO>");
buffer.append("</SEAL>");
buffer.append("<SZ_MSG></SZ_MSG>");
buffer.append("</COMMAND_INFO>");
return buffer.toString();
}
//int转byte
private static byte[] intToByte4(int i) {
byte[] targets = new byte[4];
//低位到高位
targets[0] = (byte) (i & 0xFF);
targets[1] = (byte) (i >> 8 & 0xFF);
targets[2] = (byte) (i >> 16 & 0xFF);
targets[3] = (byte) (i >> 24 & 0xFF);
return targets;
}
//byte转int
private static int byteArrayToInt(byte[] bytes) {
int value = 0;
// 由高位到低位
for (int i = 0; i < 4; i++) {
int shift = (4 - 1 - i) * 8;
value += (bytes[i] & 0x000000FF) << shift;// 往高位游
}
return value;
}
//合并byte数据
private static byte[] byteMergerAll(byte[]... values) {
int length_byte = 0;
for (int i = 0; i < values.length; i++) {
length_byte += values[i].length;
}
byte[] all_byte = new byte[length_byte];
int countLength = 0;
for (int i = 0; i < values.length; i++) {
byte[] b = values[i];
System.arraycopy(b, 0, all_byte, countLength, b.length);
countLength += b.length;
}
return all_byte;
}
}