java - 将 Stax XML 写入 ObjectOutputStream(socket.getOutputStream) 时出现 MalformedByteSequenceException

标签 java xml sockets unicode stax

我正在尝试使用 Java 中的套接字从客户端应用程序向服务器发送 xml 消息,但我不知道如何将其写入流:

        String user = "Oscar"
        String pass = "1234"

        ObjectOutputStream oos = new ObjectOutputStream(
                socket.getOutputStream());

        // Create a XMLOutputFactory
        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();

        // Create XMLEventWriter
        XMLEventWriter eventWriter = outputFactory
                        .createXMLEventWriter(oos);

        // Create a EventFactory
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();

        XMLEvent end = eventFactory.createDTD("\n");

        // Create and write Start Tag
        eventWriter.add(eventFactory.createStartDocument());
        eventWriter.add(end);

        //Se crean los atributos del tag
        ArrayList<Attribute> attributes = new ArrayList<Attribute>();
        attributes.add(eventFactory.createAttribute("nickname", user));
        attributes.add(eventFactory.createAttribute("password", pass));

        eventWriter.add(eventFactory.createStartElement
            ("", "", "authenticate",attributes.iterator(), null));
        eventWriter.add(end);

        eventWriter.add(eventFactory.createEndElement("", "", "authenticate"));

        eventWriter.add(eventFactory.createEndDocument());

我应该使用:

        eventWriter.flush();

如果我这样做,它会给我以下异常:

javax.xml.stream.XMLStreamException: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.

它说我写错了消息。那么,写入方式是怎样的呢?

消息应该是:

<?xml version="1.0" encoding="UTF-8"?>" +
<authenticate nickname="Oscar" password="1234" />

这是我在服务器上用来读取消息的代码:

//the call
this.Interpreter.readCommand(socket.getInputStream());

//the method
@SuppressWarnings({ "unchecked", "null" })
public void readCommand(InputStream command) {

    try {

        // Fabrica de entrada de XML
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();

        // Se crea un nuevo eventReader
        XMLEventReader eventReader = inputFactory.createXMLEventReader(command);

        while (eventReader.hasNext()) {

            XMLEvent event = eventReader.nextEvent();

            if (event.isStartElement()) {

                StartElement startElement = event.asStartElement();

                // Si tenemos un comando authenticate
                if (startElement.getName().getLocalPart().equals("authenticate")) {

                    CommandAuthenticate(startElement);
                }

            }

        }

    }
    catch (XMLStreamException e) {
        e.printStackTrace();
    }
}


public void CommandAuthenticate (StartElement startElement){


    String nickname = null;
    String password = null;

    // Se leen los atributos del comando
    Iterator<Attribute> attributes = startElement
                    .getAttributes();

    while (attributes.hasNext()) {

        Attribute attribute = attributes.next();

        if (attribute.getName().toString().equals("nickname")) {

            nickname = attribute.getValue();
        }

        if (attribute.getName().toString().equals("password")) {

            password = attribute.getValue();
        }

    }

    //here i call the right method for the data received

}

最佳答案

不要将流包装在 ObjectOutputStream 中。它正在向流中添加您不需要的额外信息(至少根据您当前的代码)。而且,即使你确实需要它,你也不能这样使用它。

作为旁注/一般规则,您的输入流和输出流设置通常应该匹配。如果您出于某种原因确实需要使用ObjectOutputStream,则另一端需要一个ObjectInputStream来匹配它。

关于java - 将 Stax XML 写入 ObjectOutputStream(socket.getOutputStream) 时出现 MalformedByteSequenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5965791/

相关文章:

c - sprintf/snprintf 没有正确写入缓冲区

java - 发送文件时在同一个套接字连接上发送消息

java - 与 JUnit 一起运行时,Intellij IDEA 不会在 Kotlin 断点处停止

java - HBaseProtos$SnapshotDescription 无法解析

xml - Azure 集成帐户( map - XSLT)- 使用逻辑应用将 2 个不同的 XML 合并为单个 xml

java - 是什么阻止我的 XML 文件更新?

c# - 使用 Dynamic 将 XML 反序列化为对象

java - EJB 2.1 项目中应用程序范围的资源引用

java - 为命令行 Java 应用程序创建可运行的 JAR 文件

python - 通过套接字的 python 交互式控制台没有换行符