代码贴出来大家给看看吧
这是客户端的
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中请大家顺便看下写的合理不应该如何优化谢谢大家了!!

解决方案 »

  1.   

    在调用getxml方法之前有没有读取输入流,即inputStream.read()
      

  2.   

    没有我是直接以参数和形式传到getxml方法中的
    难道这有影响?
      

  3.   

    当然有影响了,如果你先读入4个字节,然后在将inputStream交给getXml处理,那当然就少了
    要不你把调用getXml方法的整个代码都贴出来吧
      

  4.   

    public boolean handleRequest(int inlen, InputStream in, OutputStream out)
    throws IOException {
    new Xml_Parse().getxml(in);
    return true;
    }
    我就是这么调用的呀
      

  5.   

    你把获取InputStream in,然后到调用方法处理in的代码贴出来吧
      

  6.   

    private void handleConnection() {
    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");
    }
    }
    } }
    那应该就是这个了
      

  7.   

    问题就在这里了
    if (needint.equals("Y")) {
    datalen = in.readInt();// TODO
    // TODO 这个整数长度位,将来要能调整,可设位数,可设没有readInt是读取一个整数 即读取了4个字节
      

  8.   

    要不这样吧 服务器端代码不注释
    你在main函数里面写的时候
    out = new DataOutputStream(socket.getOutputStream());
    out.writeInt(4690);//插入这句           
    out.write(val);