2016-09-20 67 views
1

基本上我需要阅读的HttpServletRequest多次身体,根据我的研究,我发现,做的是最简单的方法之一是使用ContentCachingRequestWrapper如何多次读取HttpServletRequest的主体?

这里是我是如何实现它:

ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper((HttpServletRequest) request); 

try{ 
    MultipartRequest multipartRequest = new MultipartRequest(requestWrapper, ImageDirecoty, 1024*1024*5); 
    String test = requestWrapper.getReader().lines().collect(Collectors.joining(System.lineSeparator())); 
    System.out.print(test); 
} catch(IOException e){ 
    System.out.println(e.getMessage()); 
    return; 
} 

仅供参考:我正在从客户端上传一个简单的文件到服务器。

现在起初它读取请求正文很好,但在第二行,我有String test = requestWrapper来读取它的内容并将其输出到控制台我不让我的Ecplise控制台输出任何东西,我不如果有人告诉我我做错了什么,我会很感激。

+1

退房这里的解决方案http://stackoverflow.com/questions/28158826/how-to-read-a-servletinputstream-more-than-once-when-you-dont-have-control -of-t/28159475#28159475 – Palamino

回答

0

实际上最简单的方法是使用(转换响应),某种Pojo类,然后将其保存到任何你想要的。 这里是一个链接,将其转换为POJO

http://www.jsonschema2pojo.org/

也可以使用图书馆的像改造2.0,让您的HTTP调用要容易得多。

http://square.github.io/retrofit/

+0

我真的不知道如何使用Pojo,你可以在它上面放些灯吗? –