如何将HttpServletRequest转换为字符串?

13

如何将HttpServletRequest转换为String?我需要反编组HttpServletRequest,但是当我尝试时,我的程序会抛出异常。

 javax.xml.bind.UnmarshalException
 - with linked exception:
[java.io.IOException: Stream closed]
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:197)
        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)
        at com.orange.oapi.parser.XmlParsing.parse(XmlParsing.java:33)

我尝试了以下代码来反序列化HttpServletRequest

InputStreamReader is =
                new InputStreamReader(request.getInputStream());
InputStream isr = request.getInputStream();
ServletInputStream req = request.getInputStream();

我的解析器方法:

public root parse(InputStreamReader is) throws Exception {
        root mc = null;
        try {
            JAXBContext context = JAXBContext.newInstance(root.class);
            Unmarshaller um = context.createUnmarshaller();
            mc = (root) um.unmarshal(is);
        } catch (JAXBException je) {
            je.printStackTrace();
        }
        return mc;
    }

2
你需要请求(Request)、特定的HTTP头部、正文(body)等吗? - user647772
嗨,我想将HttpServlet请求转换为字符串。 - BKK
请提供一个这样的字符串可能是什么样子的例子。 - user647772
你到底想要做什么?你写了关于将请求转换为字符串的内容,但是你展示给我们的代码涉及使用JAXB通过解析请求主体来创建“根”类的对象。另外,你应该以大写字母开头命名你的类名。 - toniedzwiedz
首先,我正在尝试从客户端获取一个httpServletRequest。然后我尝试将httpServletRequest转换为字符串。在我的解析方法中,我使用JAXB进行反编组。因此,在解析时,我需要将我的httpServletRequest更改为字符串。现在我正在努力将httpServletRequest转换为字符串。 - BKK
2个回答

7

我有这样的印象,你试图在处理请求并响应客户端后从输入流中读取内容。你把代码放到哪里了?

如果你想先处理请求,稍后再进行解组,你需要首先将输入流读入一个字符串中。如果你处理的是小请求,这个方法很好用。

我建议使用像apache commons IOUtils这样的工具来帮助你完成这个任务。

String marshalledXml = org.apache.commons.io.IOUtils.toString(request.getInputStream());

同时也要记住,你需要在request.getParameter(name)request.getInputStream之间做出选择。你不能同时使用两者。


嗨,当我尝试添加一行代码String marshalledXml = IOUtils.toString(request.getInputStream());时,它显示一个错误消息,如“类型Object中的方法toString()不适用于参数(ServletInputStream)”。 - BKK
你确定你有来自org.apache.commons.io的IOUtils吗?这是自v1.0以来的支持功能:http://commons.apache.org/io/api-1.4/org/apache/commons/io/IOUtils.html#toString%28java.io.InputStream%29 - Joeri Hendrickx
通过添加一个jar文件,我可以编写String marshalledXml = org.apache.commons.io.IOUtils.toString(request.getInputStream()); 但这会影响我的DOM解析器。但是我在我的DOM解析器中没有使用这行代码。这是将httpServletRequest转换为字符串的唯一方法吗?还有其他方法吗? - BKK
你可以自己读取输入流并将其放入字符串中。但是,如果您的DOM解析器如此不稳定以至于在添加jar时崩溃,那么您可能有更严重的问题... - Joeri Hendrickx
如果我去掉这行代码:String marshalledXml = org.apache.commons.io.IOUtils.toString(request.getInputStream());,那么我的DOM解析器就可以正常工作。但是我仍然在我的项目中使用jar文件。有没有其他方法将httpServletRequest转换为字符串? - BKK
有数百种方法可以将输入流读入到字符串中。你的解析器可能失败了,因为它仍然在尝试从流中读取某些东西,而流已经耗尽了。你可以很容易地在网上找到它们。 - Joeri Hendrickx

3
String httpServletRequestToString(HttpServletRequest request) throws Exception {

    ServletInputStream mServletInputStream = request.getInputStream();
    byte[] httpInData = new byte[request.getContentLength()];
    int retVal = -1;
    StringBuilder stringBuilder = new StringBuilder();

    while ((retVal = mServletInputStream.read(httpInData)) != -1) {
        for (int i = 0; i < retVal; i++) {
            stringBuilder.append(Character.toString((char) httpInData[i]));
        }
    }

    return stringBuilder.toString();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接