代码贴出来大家给看看吧
这是客户端的
- Java code
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(); } }
这是服务端
- Java code
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(); } }
因外一个方法
- Java code
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中请大家顺便看下写的合理不应该如何优化谢谢大家了!!
------解决方案--------------------
当然有影响了,如果你先读入4个字节,然后在将inputStream交给getXml处理,那当然就少了
要不你把调用getXml方法的整个代码都贴出来吧