|
@@ -1,12 +1,6 @@
|
|
|
package com.hywa.banktest.bankframework.socketHandle;
|
|
|
|
|
|
-import java.io.BufferedReader;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.InputStreamReader;
|
|
|
-import java.io.OutputStream;
|
|
|
-import java.io.PrintWriter;
|
|
|
-import java.io.UnsupportedEncodingException;
|
|
|
+import java.io.*;
|
|
|
import java.net.ServerSocket;
|
|
|
import java.net.Socket;
|
|
|
import javax.annotation.PostConstruct;
|
|
@@ -31,8 +25,8 @@ public class BankSocket {
|
|
|
int serverPort;
|
|
|
@Value("${local.post.socket.port}")
|
|
|
int localSocketPort;
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
public Dlmaps query(Dlmaps requestPacket) {
|
|
|
Socket socket = null;
|
|
|
BufferedReader br = null;
|
|
@@ -44,16 +38,26 @@ public class BankSocket {
|
|
|
socket = new Socket(serverAddr, serverPort);
|
|
|
//socket.setSoTimeout(1000 * 58);
|
|
|
os = socket.getOutputStream();
|
|
|
- pw = new PrintWriter(os, true);
|
|
|
+
|
|
|
+ BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
|
|
|
+// pw = new PrintWriter(os, true);
|
|
|
String requestStr = dlmaps2RequestStr(requestPacket);
|
|
|
log.info("发送报文:" + requestStr);
|
|
|
- pw.write(requestStr);
|
|
|
- pw.flush();
|
|
|
+
|
|
|
+// pw.write(requestStr);
|
|
|
+// pw.flush();
|
|
|
+
|
|
|
+ bw.write(requestStr);
|
|
|
+ bw.flush();
|
|
|
// socket.shutdownOutput();
|
|
|
is = socket.getInputStream();
|
|
|
+// br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
|
|
|
+// String s = br.readLine();
|
|
|
+// String s1 = inputStream22Str(s);
|
|
|
+
|
|
|
|
|
|
result.append(inputStream2Str(is));
|
|
|
-
|
|
|
+
|
|
|
log.error("收到响应报文:" + result);
|
|
|
log.info("收到响应报文:" + result);
|
|
|
} catch (IOException e) {
|
|
@@ -65,7 +69,7 @@ public class BankSocket {
|
|
|
result.delete(result.indexOf("***"), result.length());
|
|
|
return XmlUtils.fromXML(result.toString(), Dlmaps.class);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// @PostConstruct
|
|
|
public void socketServer() {
|
|
|
|
|
@@ -83,14 +87,14 @@ public class BankSocket {
|
|
|
try {
|
|
|
socket = serverSocket.accept();
|
|
|
is = socket.getInputStream();
|
|
|
- br = new BufferedReader(new InputStreamReader(is));
|
|
|
-
|
|
|
+ br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
|
|
|
+
|
|
|
StringBuffer result = new StringBuffer(inputStream2Str(is));
|
|
|
log.info("服务端收到报文:" + result);
|
|
|
result.delete(0, result.indexOf("<"));
|
|
|
- if (result.toString().contains("***"))
|
|
|
- result.delete(result.indexOf("***"), result.length());
|
|
|
-
|
|
|
+ if (result.toString().contains("***")){
|
|
|
+ result.delete(result.indexOf("***"), result.length());
|
|
|
+ }
|
|
|
log.info("服务端处理xml报文:" + result);
|
|
|
Dlmaps dlmaps = XmlUtils.fromXML(result.toString(), Dlmaps.class);
|
|
|
BankRequsetProcess bankRequsetProcess = (BankRequsetProcess) SpringContext.getBean("bankRequsetProcess");
|
|
@@ -124,7 +128,7 @@ public class BankSocket {
|
|
|
}
|
|
|
}).start();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
private String inputStream2Str(InputStream is) throws IOException {
|
|
|
byte[] readbuf = new byte[4];
|
|
|
is.read(readbuf);
|
|
@@ -136,16 +140,32 @@ public class BankSocket {
|
|
|
String reciveString = new String(readbuf, 0, readbuf.length, "UTF-8");
|
|
|
return String.format("%04d", msglen) + reciveString;
|
|
|
}
|
|
|
-
|
|
|
+ private String inputStream22Str(String is) throws IOException {
|
|
|
+
|
|
|
+ Integer msglen = Integer.parseInt(is);
|
|
|
+ return String.format("%04d", msglen) + is;
|
|
|
+ }
|
|
|
+
|
|
|
private String dlmaps2RequestStr(Dlmaps requestPacket) throws UnsupportedEncodingException {
|
|
|
- String xmlRequest = XmlUtils.toXML(requestPacket);
|
|
|
+ String xml = XmlUtils.toXML(requestPacket);
|
|
|
+ String xmlRequest = xml;
|
|
|
StringBuffer requestPac = new StringBuffer();
|
|
|
requestPac.append("TEB").append(requestPacket.getPUB().getTRADE_ID()).append(requestPacket.getPUB().getDPT_ID()).append(xmlRequest);
|
|
|
-
|
|
|
+ System.out.println("源数据的报文:"+requestPac.toString());
|
|
|
+ System.out.println("源数据的报文长度:"+requestPac.toString().getBytes("UTF-8").length + 3);
|
|
|
StringBuffer requestStr = new StringBuffer(String.format("%04d", requestPac.toString().getBytes("UTF-8").length + 3)).append(requestPac).append("***");
|
|
|
+
|
|
|
return requestStr.toString();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) throws UnsupportedEncodingException {
|
|
|
+ String requestPac = new String("中国人".getBytes("UTF-8"));
|
|
|
+ String format = String.format("%04d", requestPac.toString().getBytes("UTF-8").length + 3);
|
|
|
+ System.out.println(format);
|
|
|
+ }
|
|
|
+
|
|
|
private void closeAll(Socket socket, BufferedReader br, InputStream is, PrintWriter pw, OutputStream os) {
|
|
|
if (null != br) {
|
|
|
try {
|
|
@@ -179,5 +199,5 @@ public class BankSocket {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|