Jfinal在Control中获得输入流有问题
用HTTPClient分别请求JFinal和普通的Servlet,将数据通过IO的形式传输。JFinal中获取不到,Servlet中获取到了。两个是相同的请求。
附上代码:HTTPClient:
public void xxx() throws Exception { StringBuffer sendStr = new StringBuffer(); sendStr.append("<?xml version="1.0" encoding="UTF-8"?>"); sendStr.append("<report_data>"); sendStr.append("<request_req>953943547334</request_req>"); sendStr.append("<request_time>2012040211394324</request_time>"); sendStr.append("<request_param>"); sendStr.append("<query_month>201203</query_month>"); sendStr.append("</request_param>"); sendStr.append("</report_data>"); BufferedReader reader = null; try { String strMessage = ""; StringBuffer buffer = new StringBuffer(); // 接报文的地址 //URL uploadServlet = new URL("http://localhost:8080/Test"); URL uploadServlet = new URL("http://localhost:8081/"); HttpURLConnection servletConnection = (HttpURLConnection) uploadServlet .openConnection(); // 设置连接参数 servletConnection.setRequestMethod("POST"); servletConnection.setDoOutput(true); servletConnection.setDoInput(true); servletConnection.setAllowUserInteraction(true); // 开启流,写入XML数据 OutputStream output = servletConnection.getOutputStream(); System.out.println("发送的报文:"); System.out.println(sendStr.toString()); output.write(sendStr.toString().getBytes()); output.flush(); output.close(); // 获取返回的数据 InputStream inputStream = servletConnection.getInputStream(); reader = new BufferedReader(new InputStreamReader(inputStream)); while ((strMessage = reader.readLine()) != null) { buffer.append(strMessage); } System.out.println("接收返回值:" + buffer); } catch (java.net.ConnectException e) { throw new Exception(); } finally { if (reader != null) { reader.close(); } } }
public void index() throws IOException { try { StringBuffer buffer = new StringBuffer(); String strMessage=""; InputStream in=getRequest().getInputStream(); System.out.println(in); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); while ((strMessage = reader.readLine()) != null) { buffer.append(strMessage); } System.out.println("接收:" + buffer.toString()); } catch (IOException e1) { e1.printStackTrace(); } String xml = "<LossRtnRequestData>" + " <LossRtnHead>" + "<REQUEST_TYPE>0206</REQUEST_TYPE>" + "<uuid> 4028809732616a50013267d8d75e0092</uuid>" + "<SENDER>tengxin</SENDER >" + "<SERVER_VERSION>v1.3.0.0</SERVER_VERSION>" + "<RESPONSE_CODE>1</RESPONSE_CODE>" + "<ERROR_MESSAGE>请求成功</ERROR_MESSAGE>" + "</LossRtnHead>" + "</LossRtnRequestData>"; String bidInfoXML = getPara("bidinfo", ""); System.out.println("--------------------获得竞价信息XML---------------n" + bidInfoXML); renderText(xml, "application/xml"); }
发送的报文: <?xml version="1.0" encoding="UTF-8"?><report_data><request_req>953943547334</request_req><request_time>2012040211394324</request_time><request_param><query_month>201203</query_month></request_param></report_data> 接收返回值:<LossRtnRequestData> <LossRtnHead><REQUEST_TYPE>0206</REQUEST_TYPE><uuid> 4028809732616a50013267d8d75e0092</uuid><SENDER>tengxin</SENDER ><SERVER_VERSION>v1.3.0.0</SERVER_VERSION><RESPONSE_CODE>1</RESPONSE_CODE><ERROR_MESSAGE>请求成功</ERROR_MESSAGE></LossRtnHead></LossRtnRequestData>
JFinal项目控制台显示的结果:
JFinal action report -------- 2015-05-04 09:38:30 ------------------------------ Controller : com.enquiry.control.IndexControl.(IndexControl.java:1) Method : index Parameter : <?xml version="1.0" encoding="UTF-8"?><report_data><request_req>953943547334</request_req><request_time>2012040211394324</request_time><request_param><query_month>201203</query_month></request_param></report_data> -------------------------------------------------------------------------------- org.apache.catalina.connector.CoyoteInputStream@21f11507 接收: --------------------获得竞价信息XML---------------
==================================================================
普通Servlet接收:
/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String strMessage=""; StringBuffer buffer = new StringBuffer(); BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream())); while ((strMessage = reader.readLine()) != null) { buffer.append(strMessage); } System.out.println("接收:" + buffer.toString()); }
HTTPClient项目控制台显示的结果:
发送的报文: <?xml version="1.0" encoding="UTF-8"?><report_data><request_req>953943547334</request_req><request_time>2012040211394324</request_time><request_param><query_month>201203</query_month></request_param></report_data>
Servlet项目控制台显示的结果:
接收:<?xml version="1.0" encoding="UTF-8"?><report_data><request_req>953943547334</request_req><request_time>2012040211394324</request_time><request_param><query_month>201203</query_month></request_param></report_data>
==============================================================
Servlet接收是正常的,为什么JFinal不能正常接收。@JFinal
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
引用来自“IMBA__Xiao8”的评论
把devMode设成false就可以了
把devMode设成false就可以了
没有异常。用这种方式获取到的是空的
有什么异常抛出没? 在Controller中用一下String data = HttpKit.readIncommingRequestData(getRequest());