HTTPServlet Request.getInputStream() 总是接收空行

发布于 2024-10-19 21:52:26 字数 3044 浏览 3 评论 0原文

客户:

    public List<String> post(List<String> toWrite){
        String result = "";
        List<String> allResults = new ArrayList<String>();

        try {
            openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            initializeOutputStream();
            for(int i = 0; i < toWrite.size(); i++){
                out.write(toWrite.get(i));
                out.newLine();
            }
            System.out.println(connection.getResponseCode());
            System.out.println(connection.getResponseMessage());
            initializeInputStream();
            while((result = in.readLine()) != null){
                allResults.add(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeConnection();
        }

        return allResults;
    }

One of the attempts at the host:

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        List<String> incoming = new ArrayList<String>();

//      BufferedReader in = req.getReader();
//
//      String tmp = in.readLine();

        resp.setContentType("text/plain");
        PrintWriter out = resp.getWriter();

        StringBuilder stringBuilder = new StringBuilder();  
        BufferedReader bufferedReader = null;  
        try {  
          InputStream inputStream = req.getInputStream();  
          if (inputStream != null) {  
           bufferedReader = new BufferedReader(new InputStreamReader(  
        inputStream));  
           char[] charBuffer = new char[128];  
           int bytesRead = -1;  
           //while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
           while(bytesRead != -1){
            stringBuilder.append(charBuffer, 0, bytesRead);  
           }  
          } else {  
           stringBuilder.append("");  
          }  
        } catch (IOException ex) {  
          throw ex;  
        } finally {  
          if (bufferedReader != null) {  
           try {  
            bufferedReader.close();  
           } catch (IOException ex) {  
            throw ex;  
           }  
          }  
        }  
        String body = stringBuilder.toString();  
        System.out.println(body);

        out.println(body);

//      BufferedReader in = new BufferedReader(new InputStreamReader(req.getInputStream()));        
//      
//      String tmp = "";
//      
//      //while(!(in.ready())){}
//      
//      while((tmp = in.readLine()) != null){
//          System.out.println(tmp);
//      }
//
//
//      out.println(tmp);

        out.println("end");

    }

请注意注释掉的行 - 这是我尝试从客户那里获取东西的许多其他尝试之一。

servlett 中的 System.out.printlns 和 out.printlns 都返回空行。程序结束时的“结束”返回没有问题。这不是在客户端读回多行的问题 - 如果我放置多个 out.println,那么我可以很好地读取它们。输入流的 system.out.println() 也返回空白。状态码是200,所以看起来没有连接错误。

有人吗?

The client:

    public List<String> post(List<String> toWrite){
        String result = "";
        List<String> allResults = new ArrayList<String>();

        try {
            openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            initializeOutputStream();
            for(int i = 0; i < toWrite.size(); i++){
                out.write(toWrite.get(i));
                out.newLine();
            }
            System.out.println(connection.getResponseCode());
            System.out.println(connection.getResponseMessage());
            initializeInputStream();
            while((result = in.readLine()) != null){
                allResults.add(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeConnection();
        }

        return allResults;
    }

One of the attempts at the host:

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        List<String> incoming = new ArrayList<String>();

//      BufferedReader in = req.getReader();
//
//      String tmp = in.readLine();

        resp.setContentType("text/plain");
        PrintWriter out = resp.getWriter();

        StringBuilder stringBuilder = new StringBuilder();  
        BufferedReader bufferedReader = null;  
        try {  
          InputStream inputStream = req.getInputStream();  
          if (inputStream != null) {  
           bufferedReader = new BufferedReader(new InputStreamReader(  
        inputStream));  
           char[] charBuffer = new char[128];  
           int bytesRead = -1;  
           //while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
           while(bytesRead != -1){
            stringBuilder.append(charBuffer, 0, bytesRead);  
           }  
          } else {  
           stringBuilder.append("");  
          }  
        } catch (IOException ex) {  
          throw ex;  
        } finally {  
          if (bufferedReader != null) {  
           try {  
            bufferedReader.close();  
           } catch (IOException ex) {  
            throw ex;  
           }  
          }  
        }  
        String body = stringBuilder.toString();  
        System.out.println(body);

        out.println(body);

//      BufferedReader in = new BufferedReader(new InputStreamReader(req.getInputStream()));        
//      
//      String tmp = "";
//      
//      //while(!(in.ready())){}
//      
//      while((tmp = in.readLine()) != null){
//          System.out.println(tmp);
//      }
//
//
//      out.println(tmp);

        out.println("end");

    }

Please note the commented out lines- thats one of the many other attempts I've tried to get stuff from the client.

System.out.printlns and out.printlns from the servlett all return a blank line. The "end" at the end of the program returns without problem. It is not a problem of reading multiple lines back on the client side- if I put multiple out.println's, then I read them fine. The system.out.println() for the inputstream also returns blank. The status code is 200, so there seems to be no connection errors.

Anyone?

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

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

发布评论

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

评论(2

鹿港小镇 2024-10-26 21:52:26

看起来你的 while 循环在这里没有做任何事情,因为在你的情况下, bytesRead 总是 -1 因此它根本不会进入循环,而且,你不使用你的bufferedReader 完全从输入流中读取:-

    int bytesRead = -1;
    while (bytesRead != -1) {
        stringBuilder.append(charBuffer, 0, bytesRead);
    }

试试这个:-

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/plain");
    PrintWriter out = resp.getWriter();

    StringBuilder stringBuilder = new StringBuilder(1000);
    Scanner scanner = new Scanner(req.getInputStream());
    while (scanner.hasNextLine()) {
        stringBuilder.append(scanner.nextLine());
    }

    String body = stringBuilder.toString();

    System.out.println(body);
    out.println(body);

}

It seems like your while loop doesn't do anything here, because in your case, bytesRead is always -1 thus it will never get into the loop at all, and further, you don't use your bufferedReader at all to read from the input stream:-

    int bytesRead = -1;
    while (bytesRead != -1) {
        stringBuilder.append(charBuffer, 0, bytesRead);
    }

Try this:-

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/plain");
    PrintWriter out = resp.getWriter();

    StringBuilder stringBuilder = new StringBuilder(1000);
    Scanner scanner = new Scanner(req.getInputStream());
    while (scanner.hasNextLine()) {
        stringBuilder.append(scanner.nextLine());
    }

    String body = stringBuilder.toString();

    System.out.println(body);
    out.println(body);

}
金兰素衣 2024-10-26 21:52:26

将以下行替换

while(bytesRead != -1){
        stringBuilder.append(charBuffer, 0, bytesRead);  
}  

bufferedReader.read(charBuffer);
String str = new String(charBuffer);
System.out.println(charBuffer);

希望它有效。

Replace below lines

while(bytesRead != -1){
        stringBuilder.append(charBuffer, 0, bytesRead);  
}  

with

bufferedReader.read(charBuffer);
String str = new String(charBuffer);
System.out.println(charBuffer);

Hope it works.

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