public synchronized String sendAskToUnionPay(String encryptText) throws Exception {
Socket socket = null;
OutputStream output = null; // 输入流
DataInputStream input = null;
InetSocketAddress inetSocketAddress = null;
String text = null;
int communicateTime = 0; // 当前已尝试连接的次数
try {
Properties props = FTPConfiger.getProperties();//获取配置文件
String socketIP = props.getProperty("socketIP");// 从配置文件中获取参数IP
int socketProt = Integer.parseInt(props.getProperty("socketProt"));// 端口
int socketConnectionCount = Integer.parseInt(props.getProperty("socketConnectionCount"));// 最大连接次数
int socketConnectionTime = Integer.parseInt(props.getProperty("socketConnectionTime"));// 超时时间
int socketWateServerTime = Integer.parseInt(props.getProperty("socketWateServerTime"));// 超时时间
String bankSocketEndTag = props.getProperty("bankSocketEndTag");// 从配置文件中获取参数IP
inetSocketAddress = new InetSocketAddress(socketIP, socketProt);
// ------------创建连接------------
socket = new Socket();
while (communicateTime < socketConnectionCount) {
socket.connect(inetSocketAddress, socketConnectionTime * 1000);
communicateTime++;
if (socket.isConnected()) {
System.out.println("连接服务器成功!");
log.debug("连接服务器成功!");
break;
}else{
log.debug("连接服务器失败!");
throw new BusinessException("连接服务器失败!");
}
}
if (socket.isConnected()) {
output = socket.getOutputStream();//基于Soacket返回输出流
output.write(encryptText.getBytes());
output.flush();// 发送信息至银联
input = new DataInputStream(socket.getInputStream());//基于Soacket返回输入流
int avali = 0;
for (int i = 0;i < socketWateServerTime; i++){
avali = input.available();
if (avali == 0){
Thread.sleep(1000);
System.out.println("银行没有返回信息,please wait......");
}else{
Util comUtil = Util.getUtil();
// ---------------------------------获取流的头信息,
byte aval[] = new byte[6];
input.read(aval,0,6);
String strCount = comUtil.decodeStr(aval, "GB2312");
System.out.println("服务器返回的头信息是 :" + strCount);
int count = Integer.parseInt(strCount.substring(0,4));//记录数
String returnTitle = strCount.substring(4,6);
// ---------------------------------
if (count > 0 && (returnTitle.equals("00") || returnTitle.equals("04"))){
byte tmpdata[] = new byte[count * 2 * 1660 ];
int nowIndex = 0;
byte bTmp;
// ------------------------------------循环从流中读取数据,知道读取不到为止
while ((bTmp = (byte)input.read()) != -1) {
if (input.available() ==0) {
for (int j= 0;j<5;j++){
if (input.available() != 0) break;
else Thread.sleep(1000);
}
log.debug("没有获取到数据,可能是因为网络现在过于拥挤,请稍后再试!!!");
throw new BusinessException("没有获取到数据,可能是因为网络现在过于拥挤,请稍后再试!!!");
}
tmpdata[nowIndex] = bTmp;
nowIndex ++;
}
if (output != null) output.close();
if (input != null) input.close();
if (socket != null) socket.close();
// ------------------------------------
if (tmpdata.length > 0)
text = comUtil.decodeStr(tmpdata,"GB2312").trim();
else
throw new BusinessException("银联返回的数据长度为null......要怎么办呢?");
// --------------------------------------
int endTagNum = bankSocketEndTag.length();
int textNum =text.length();
if (text.substring(textNum - endTagNum,textNum).equals(bankSocketEndTag)){
text = text.substring(0,textNum - endTagNum);
}
text = strCount + text;
log.debug("获取到的数据是 >>>>>>>:" + text);
System.out.println("获取到的数据是 >>>>>>>:" + text);
break;
}
}
}
}
return text;
} catch (UnknownHostException e) {
log.error("连接中断,中断的原因是: " + e.getMessage());
throw new BusinessException("连接中断,中断的原因是: " + e.getMessage());
} catch (IOException e) {
log.error("连接中断,中断的原因是: " + e.getMessage());
throw new BusinessException("连接中断,中断的原因是: " + e.getMessage());
} finally {
if (output != null) output.close();
if (input != null) input.close();
if (socket != null) socket.close();
}
}