java后台调用SOE时,报异常java.io.IOException: Attempted read from closed stream
2016-08-10 09:23
651 查看
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<NameValuePair> params = new ArrayList<NameValuePair>();
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中添加了此句话的表达式监听,已经执行过一次,在代码中真正执行前流已经关闭,将表达式监听中的表达式清除,问题解决。
代码诸如此类
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<NameValuePair> params = new ArrayList<NameValuePair>();
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.io.IOException: Attempted read from closed stream. 异常,解决
- java.io.IOException: Attempted read from closed stream解决
- java.io.IOException: Attempted read from closed stream.
- java.io.IOException: Attempted read from closed stream.
- java.io.IOException: Attempted read from closed stream
- java.io.IOException: Attempted read on closed stream.
- java.io.IOException: Attempted read on closed stream
- java.io.IOException: Attempted read on closed stream.可能原因及解决方案
- java.io.IOException:stream closed 异常的原因及处理
- java.io.IOException:stream closed 异常的原因及处理
- java.io.IOException:stream closed 异常的原因及处理
- hessian协议从rpc服务获取inputStream流下载文件的方式,出现异常 java.io.IOException: stream is closed
- java.io.IOException:stream closed 异常的原因及处理
- java.io.IOException:stream closed 异常的原因及处理
- 【Java】异常 java.io.IOException: Stream closed 的解决办法
- java.io.IOException:stream closed 异常的原因及处理
- 【httpclient】Attempted read from closed stream
- Attempted read from closed stream.的解决方法
- java.sql.SQLException: Io 异常: Got minus one from a read call
- 混淆打包出现 java.io.IOException: Can't write、Can't read 异常