java后台调用SOE时,报异常java.io.IOException: Attempted read from closed stream

java后台调用SOE时,报异常java.io.IOException: Attempted read from closed stream


代码诸如此类


public void test(HttpServletRequest request) throws Exception{

		String url = "http://192.168.1.122:6080/arcgis/rest/services/binzhou/MapServer/exts/RSOESDJT/QueryBuffer";
		HttpClient client = new DefaultHttpClient();
		HttpPost req= new HttpPost(url);
		List params = new ArrayList();
		params.add(new BasicNameValuePair("f","json"));
		params.add(new BasicNameValuePair("WhereClause","东赵"));
		params.add(new BasicNameValuePair("BufferRadius","1000"));

		try {
            //设置http Post请求参数 
            HttpEntity entity = new UrlEncodedFormEntity(params);
            req.setEntity(entity); 
            HttpResponse response=client.execute(req);
            if(response.getStatusLine().getStatusCode()==200){//如果状态码为200,就是正常返回
                String result=EntityUtils.toString(response.getEntity());
                System.out.println(result);            
                //需要对返回的结果进行分析,判断新增记录是成功还是失败
                //如果成功,则进行后续的分析
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            //进行处理操作            
        } catch (IOException e) {
            //进行处理操作
        } 
}

在执行到

String result=EntityUtils.toString(response.getEntity());
这一行时,提示标题所述异常

java.io.IOException: Attempted read from closed stream.
at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:172)
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:138)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.readLine(BufferedReader.java:317)
at java.io.BufferedReader.readLine(BufferedReader.java:382)


原因解释:EntityUtils.toString(entity)方法只能调用一次,entity所得到的流是不可重复读取的也就是说所得的到实体只能一次消耗完,不能多次读取。

发现之前在eclipse中添加了此句话的表达式监听,已经执行过一次,在代码中真正执行前流已经关闭,将表达式监听中的表达式清除,问题解决。

你可能感兴趣的:(java开发,SOE相关)