private static String tcpSendDataToEsb(String host, int port, int timeout,String content) throws Exception {
Socket socket = null;
try {
socket = new Socket();
InetSocketAddress address = new InetSocketAddress(host, port);
socket.connect(address, timeout);
OutputStream outputStream = socket.getOutputStream();
byte contentbs[] = content.getBytes();
outputStream.write(ByteUtil.inttoBytes(contentbs.length));
outputStream.write(contentbs);
outputStream.flush();
IntputStream is = socket.getinputStream();
DataInputStream dis = new DataInputStream(is);
int len = 0;
byte[] tembs = new byte[1024];
ByteArrayOutputStream out = new ByteArrayOutputStream();
while((len = dis.read(tembs)) != -1) {
out.wirte(tembs, 0 , len);
}
out.close();
byte[] lenresult = new byte[4]; // 响应长度
byte[] result = out.toByteArray();// 响应内容
// System.arraycopy(result, 0 , lenresult, 0,4);
outputStream.close();
is.close();
dis.close();
return new String(result, 4, result.length - 4, "GBK");
} catch(ArrayIndexOutOfBoundsException e) {
log.error("调用esb接口发送内容为:" + content + "出现异常:" + e.getStackTrace()[0], e);
} finally {
if(null != socket && !socket.isClosed()) {
try {
socket.close();
} catch(IOException e) {
log.error("发送内容:" + content + "到esb关闭socket连接出现异常:" + e.getStackTrace()[0], e);
}
}
}
}