Jfinal在Control中获得输入流有问题

IMBA__Xiao8 发布于 2015/05/04 09:43
阅读 1K+
收藏 0

        用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();
			}

		}
	}



JFinal接收:

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");
	}



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>
接收返回值:<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






加载中
0
JFinal
JFinal
有什么异常抛出没? 在Controller中用一下String data =  HttpKit.readIncommingRequestData(getRequest());
IMBA__Xiao8
IMBA__Xiao8
没有异常。用这种方式获取到的是空的
0
IMBA__Xiao8
IMBA__Xiao8
把devMode设成false就可以了
0
JFinal
JFinal

引用来自“IMBA__Xiao8”的评论

把devMode设成false就可以了
JFinal action report 从 request 中读取数据时影响到了后续的数据读取
返回顶部
顶部