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

发布于 2021-11-29 14:39:52 字数 6890 浏览 896 评论 4

        用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






如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

坏尐絯 2021-11-29 16:48:20

引用来自“IMBA__Xiao8”的评论

把devMode设成false就可以了

归途 2021-11-29 16:48:04

把devMode设成false就可以了

把回忆走一遍 2021-11-29 16:40:09

没有异常。用这种方式获取到的是空的

尐偏执 2021-11-29 14:53:38

有什么异常抛出没? 在Controller中用一下String data =  HttpKit.readIncommingRequestData(getRequest());

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文