请求的代码
public static synchronized String socketPost(String clientIp, String clientPort, String xml, String filePath) throws Exception {
String rs = "";
System.out.println(xml);
if (clientIp == null || "".equals(clientIp) || clientPort == null || "".equals(clientPort)) {
throw new Exception("数据同步地址信息有误,请联系管理员!");
}
int clientPortInt = Integer.parseInt(clientPort);
logger.info("clientIp:" + clientIp + " clientPort:" + clientPort);
Socket s = null;
OutputStream out = null;
InputStream in = null;
try {
s = new Socket(clientIp, clientPortInt);
s.setSendBufferSize(8192);
s.setReceiveBufferSize(8192);
s.setTcpNoDelay(true);
s.setSoTimeout(30000);
s.setKeepAlive(true);
out = s.getOutputStream();
in = s.getInputStream();
out.write(xml.getBytes("GBK"));
out.flush();
if (in.available() > 0) {
Thread.sleep(100);
}
//前8字节报文长度
byte[] byte8 = new byte[8];
in.read(byte8);
logger.info("===========================前8字节报文长度" + new String(byte8));
Integer int8 = Integer.parseInt(new String(byte8, "GBK"));
logger.info("===========================" + int8);
//报文正文
byte[] byteXml = new byte[int8];
in.read(byteXml);
rs = new String(byteXml, "GBK");
logger.info("===========================报文正文" + rs);
//后续报文标记3位
byte[] byteEnd = new byte[3];
in.read(byteEnd);
String strEnd = new String(byteEnd, "GBK");
logger.info("===========================后续报文标记3位" + strEnd);
if(strEnd.equals("FIL")){
//30位文件名
byte[] byte30 = new byte[30];
in.read(byte30);
String fileName = new String(byte30, "GBK").trim();
if(StringUtils.isBlank(fileName)){
fileName = UUID.randomUUID().toString()+".jpg";
}
logger.info("==========================="+fileName);
//前8字节文件长度
byte8 = new byte[8];
in.read(byte8);
int8 = Integer.parseInt(new String(byte8,"GBK"));
logger.info("==========================="+int8);
//文件
byte[] byteFile = readStream(in,int8);
//本地文件
File fileDir = new File(filePath);
if(!fileDir.exists()||!fileDir.isDirectory()) {
fileDir.mkdirs();
}
File file = new File(filePath + File.separatorChar + fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(byteFile);
fos.flush();
fos.close();
}
返回值开始的一部分数据是正常的剩下的就全是NUL