您的位置:首页 > 其它

向ESB发送和接收数据

2016-10-26 14:49 417 查看
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);

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: