6

我试图找到一种方法来避免与我在封闭流上阅读的事实相关的 IOException。

我正在调用一个返回 Stream 的 web 服务方法:

      InputStream stream = callRestWebService();
     try {
         parkingState = objectMapper.readValue(stream, ParkingState.class);
      }   catch (IOException e) {
        throw new ParkingMeasurementProviderException("Could not retrieve data.", e);
      } 

然后,我有我的 Web 服务方法,我在其中关闭了 get 连接:

public InputStream callRestWebService() {
    int parkingId = 2803;
    String endpointURL = REST_ENDPOINT + URI_INFO_PATH + parkingId + "/parkingState";
    InputStream inputStream = null;

    // Create an instance of HttpClient.
    HttpClient httpclient = new HttpClient();

    // Create a method instance.
    GetMethod getMethod = new GetMethod(endpointURL);
    getMethod.addRequestHeader("accept", "application/json");

    try {
        // Execute the method.
        int statusCode = httpclient.executeMethod(getMethod);
        inputStream = getMethod.getResponseBodyAsStream();

     } catch (IOException e) {
       e.printStackTrace();
    } finally {
        // Release the connection.
        getMethod.releaseConnection();
    }
    return inputStream;
}

有没有办法在不删除以下内容的情况下避免出现此异常:getMethod.releaseConnection();

堆栈跟踪:

    Disconnected from the target VM, address: '127.0.0.1:62152', transport: 'socket'
    at be.ixor.itg.server.service.parking.hermesWS.HermesWSParkingControllerMeasurementProvider.getHermesMechelenData(HermesWSParkingControllerMeasurementProvider.java:126)
    at be.ixor.itg.server.service.parking.hermesWS.Main.main(Main.java:14)
Caused by: java.io.IOException: Attempted read on closed stream.
    at org.apache.commons.httpclient.AutoCloseInputStream.isReadAllowed(AutoCloseInputStream.java:183)
    at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:86)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager$RewindableInputStream.read(XMLEntityManager.java:2977)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:702)
    at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:232)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)
    at be.ixor.itg.server.service.parking.hermesWS.HermesWSParkingControllerMeasurementProvider.getHermesMechelenData(HermesWSParkingControllerMeasurementProvider.java:116)
    ... 1 more
4

2 回答 2

6

因为您在 finally 块中调用 releaseConnection() ,所以输入流将不再可用。

如果您不希望内容很大,您可以将输入流中的数据读入缓冲区并返回缓冲区而不是输入流。否则,您将需要更改代码以允许被调用方在释放连接之前处理来自输入流的数据。

于 2013-06-14T14:18:50.920 回答
0
BufferedReader br = new BufferedReader(new     
InputStreamReader((response.getEntity().getContent())));
String response = br.readLine();
System.out.println("response" + response );

这段代码对我有用。

于 2018-10-09T11:41:12.443 回答