代码贴出来大家给看看吧
这是客户端的
public static List elementMap = new ArrayList(); public static void main(String[] args) {
File file = new File("G:\\Workspace\\InterfaceGZRC\\doc\\TCO1.xml");
byte[] val = new byte[4690]; BufferedReader in;
DataOutputStream out;
try {
InputStream input = new FileInputStream(file);
input.read(val, 0, val.length);
for (int i = 0; i < val.length; i++) {
System.out.println((char)val[i]);
}
Socket socket = new Socket("localhost", 5008);
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
out = new DataOutputStream(socket.getOutputStream());
out.write(val); } catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这是服务端 public void getxml(InputStream in) {
byte[] val = new byte[4690];
try {
//InputStream in = new FileInputStream(file);
InputStream inputStream=new DataInputStream(in);
inputStream.read(val, 0, val.length);
inputStream.close();
for (int i = 0; i < val.length; i++) {
System.out.println((char)val[i]);
}
ByteArrayInputStream bais = new ByteArrayInputStream(val);
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(bais);
Element rootElement = document.getRootElement();
new Xml_Parse().removePrefix(rootElement); } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}因外一个方法
private void removePrefix(Element element) { if (element.getData().toString().trim() != null
&& !element.getData().toString().trim().equals("")) {
elementMap.add(0, ":" + element.getQName().getName() + ":"
+ element.getData().toString());
}
if (element != null && element.getQName().getName().length() > 0) {
element.setQName(new QName(element.getQName().getName()));
Iterator elementIterator = element.elementIterator();
while (elementIterator.hasNext()) {
this.removePrefix((Element) elementIterator.next());
}
}大至就是这样的 但是我接收后前四位却没有了
客户端打印:
<
?
x
m
l
v
e
r
s
i
o
n
服务端打印:
l
v
e
r
s
i
o
n
前四位直接没了
也不知道是什么原因
快一年没碰过JAVA了写这个程序本意是解析XML文件然后放到List中请大家顺便看下写的合理不应该如何优化谢谢大家了!!
这是客户端的
public static List elementMap = new ArrayList(); public static void main(String[] args) {
File file = new File("G:\\Workspace\\InterfaceGZRC\\doc\\TCO1.xml");
byte[] val = new byte[4690]; BufferedReader in;
DataOutputStream out;
try {
InputStream input = new FileInputStream(file);
input.read(val, 0, val.length);
for (int i = 0; i < val.length; i++) {
System.out.println((char)val[i]);
}
Socket socket = new Socket("localhost", 5008);
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
out = new DataOutputStream(socket.getOutputStream());
out.write(val); } catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这是服务端 public void getxml(InputStream in) {
byte[] val = new byte[4690];
try {
//InputStream in = new FileInputStream(file);
InputStream inputStream=new DataInputStream(in);
inputStream.read(val, 0, val.length);
inputStream.close();
for (int i = 0; i < val.length; i++) {
System.out.println((char)val[i]);
}
ByteArrayInputStream bais = new ByteArrayInputStream(val);
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(bais);
Element rootElement = document.getRootElement();
new Xml_Parse().removePrefix(rootElement); } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}因外一个方法
private void removePrefix(Element element) { if (element.getData().toString().trim() != null
&& !element.getData().toString().trim().equals("")) {
elementMap.add(0, ":" + element.getQName().getName() + ":"
+ element.getData().toString());
}
if (element != null && element.getQName().getName().length() > 0) {
element.setQName(new QName(element.getQName().getName()));
Iterator elementIterator = element.elementIterator();
while (elementIterator.hasNext()) {
this.removePrefix((Element) elementIterator.next());
}
}大至就是这样的 但是我接收后前四位却没有了
客户端打印:
<
?
x
m
l
v
e
r
s
i
o
n
服务端打印:
l
v
e
r
s
i
o
n
前四位直接没了
也不知道是什么原因
快一年没碰过JAVA了写这个程序本意是解析XML文件然后放到List中请大家顺便看下写的合理不应该如何优化谢谢大家了!!
难道这有影响?
要不你把调用getXml方法的整个代码都贴出来吧
throws IOException {
new Xml_Parse().getxml(in);
return true;
}
我就是这么调用的呀
String line;
DataInputStream in = null;
DataOutputStream out = null;
try { if (!validateConnectingServer(client)) {
traceAlways(Logger.LOG_LEVEL_DEBUG, "Thread aborting");
log.debug("Thread aborting");
return;
} if (maxSessions > 0 && connCount > maxSessions) {
traceAlways(Logger.LOG_LEVEL_ERROR,
"Maximum session count reached. Thread aborting");
log
.error("Maximum session count reached. Thread aborting");
return;
} try {
in = new DataInputStream(new BufferedInputStream(client
.getInputStream()));
out = new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
trace(Logger.LOG_LEVEL_ERROR,
"Unable to get input/output streams from request socket.");
log
.error("Unable to get input/output streams from request socket.");
trace("Thread aborting");
log.debug("Thread aborting");
return;
} // read length of data block
try {
log.info(in.available());
int datalen = 0;
if (needint.equals("Y")) {
datalen = in.readInt();// TODO
// TODO 这个整数长度位,将来要能调整,可设位数,可设没有
// allocate byte array and
trace("Length of Request11: " + datalen);
log.debug("Length of Request11: " + datalen);
}
System.out.println("----------1---------");
IServerWorker handler = null;
try {
Constructor construct = wrkclass
.getConstructor(new Class[] {});
handler = (IServerWorker) construct
.newInstance(new Object[] {});
} catch (Exception ex) {
ex.printStackTrace();
}
ByteArrayOutputStream bout = new ByteArrayOutputStream(); if (handler != null) {
boolean result = handler.handleRequest(datalen, in,
bout);
if (!result) {
terminate();
}
} trace("Result-Length: " + bout.size());
log.debug("Result-Length: " + bout.size());
if (needint.equals("Y")) {
out.writeInt(bout.size());
}
out.write(bout.toByteArray());
out.close();
trace("Request-Result written");
log.debug("Request-Result written");
} catch (IOException e) {
traceAlways(Logger.LOG_LEVEL_ERROR, "Read failed");
log.error("Read failed");
trace("Read failed");
log.debug("Read failed");
trace("Thread aborting");
log.debug("Thread aborting");
return;
}
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
log
.debug("socket close failed." + " "
+ e.getMessage());
}
trace("Socket-connection closed");
log.debug("Socket-connection closed");
trace("Thread exiting");
log.debug("Thread exiting");
}
}
} }
那应该就是这个了
if (needint.equals("Y")) {
datalen = in.readInt();// TODO
// TODO 这个整数长度位,将来要能调整,可设位数,可设没有readInt是读取一个整数 即读取了4个字节
你在main函数里面写的时候
out = new DataOutputStream(socket.getOutputStream());
out.writeInt(4690);//插入这句
out.write(val);