Java 分析时发生InputStream异常

Java 分析时发生InputStream异常,java,Java,我从客户机获得一个HttpservletRequest,我正在尝试解析该请求(使用JAXB、DOM)。为此,我使用以下代码 String str_request = IOUtils.toString(request.getInputStream()); System.out.println("String is " + str_request); requestStream = new ByteArrayInputStream(str_request.getBytes()); 我的问题是,当我

我从客户机获得一个HttpservletRequest,我正在尝试解析该请求(使用JAXB、DOM)。为此,我使用以下代码

String str_request = IOUtils.toString(request.getInputStream());
System.out.println("String is " + str_request);
requestStream = new ByteArrayInputStream(str_request.getBytes());
我的问题是,当我将requestStream传递给我的解析器方法时,一个方法工作正常,但另一个失败。我猜inputStream有问题,但我无法解决这个问题。有谁能提出解决这个问题的办法吗

Dom解析器方法:

public String parseMethodName(InputStream request) throws SAXException,
                                                          IOException,
                                                          ParserConfigurationException {

    System.out.println("in parseMethodName");
    DocumentBuilderFactory dbFactory =
        DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    System.out.println("bfor parse");
    Document doc = dBuilder.parse(request);
    System.out.println("After parse");
    methodName =
            doc.getElementsByTagName("methodName").item(0).getTextContent();
}
System.out.println("in parse ******");
JAXBContext context = JAXBContext.newInstance(MethodCall.class);
System.out.println("bfor unmarshall ****");
Unmarshaller um = context.createUnmarshaller();
System.out.println("After unmarshall ****");
mc = (MethodCall) um.unmarshal(is);
JAXB解析器方法:

public String parseMethodName(InputStream request) throws SAXException,
                                                          IOException,
                                                          ParserConfigurationException {

    System.out.println("in parseMethodName");
    DocumentBuilderFactory dbFactory =
        DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    System.out.println("bfor parse");
    Document doc = dBuilder.parse(request);
    System.out.println("After parse");
    methodName =
            doc.getElementsByTagName("methodName").item(0).getTextContent();
}
System.out.println("in parse ******");
JAXBContext context = JAXBContext.newInstance(MethodCall.class);
System.out.println("bfor unmarshall ****");
Unmarshaller um = context.createUnmarshaller();
System.out.println("After unmarshall ****");
mc = (MethodCall) um.unmarshal(is);
我得到以下例外情况:

javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException: Premature end of file.]
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:481)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:199)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:168)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:184)

您不应该将输入流转换为字符串,然后再转换回字节数组。这只是要求丢失数据,特别是当您在将字符串转换为字节数组时使用平台默认编码时

查看您使用的
IOUtils
类是否包含将
InputStream
完全读取到
字节[]
的方法,并将其用作
ByteArrayInputStream
的基础

请注意,如果要多次读取数据,则需要重置流,或者围绕同一字节数组创建一个新流