java.lang.IllegalStateException:在使用ServletInputStream时不能使用BufferedReader

时间:2017-03-03 22:32:00

标签: java http post

Java专家可以帮我解决这个问题,我正在解决下面的错误

java.lang.IllegalStateException:ServletInputStream正在使用时无法使用BufferedReader

我正在调用REST服务网址,使用服务器端java代码发布一些数据,而不是通过浏览器发布。

 try{
    String urlLocation = "http://myserver/james/dev/hello.nsf/services.xsp/test";
    URL url = new URL(urlLocation);
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    connection.setRequestMethod("POST"); 
        connection.setAllowUserInteraction(false); 
        connection.setDoOutput(true); 
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); 
        //connection.setRequestProperty( "Content-length", Integer.toString(content.length())); 
        DataOutputStream out = new DataOutputStream (connection.getOutputStream ());
        out.writeBytes (getXML());
        out.flush ();
        out.close ();
        connection.disconnect();

        in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 

        String line;
        while( (line = in.readLine()) !=null) {
            System.out.println(line);
            } 
        in.close();
        }
        catch(Exception e){
            System.out.println("Error from 2nd try statement");
            e.printStackTrace();
            e.toString();
        }

任何帮助将不胜感激......

1 个答案:

答案 0 :(得分:0)

似乎是这一行

        in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 

我注意到BufferedReader没有被实例化,你正在重用一个实例变量in

我复制了您的代码并将其更改为此代码,我可以毫无问题地发出多个请求。

BufferedReader in = new BufferedReader(
   new InputStreamReader(connection.getInputStream()));