HTTPServlet Request.getInputStream()始终接收空白行 [英] HTTPServlet Request.getInputStream() always receiving blank line

查看:232
本文介绍了HTTPServlet Request.getInputStream()始终接收空白行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

客户:

    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和out.printlns都返回一个空行.程序末尾的"end"没有问题地返回.在客户端读回多行不是问题-如果我将多行out.println放进去,那么我就读得很好.输入流的system.out.println()也返回空白.状态代码为200,因此似乎没有连接错误.

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.

有人吗?

推荐答案

您的while循环似乎在这里什么也不做,因为在您的情况下,bytesRead始终为-1,因此它将永远不会进入循环而且,您根本不会使用bufferedReader来读取输入流:-

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);
    }

尝试一下:-

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);

}

这篇关于HTTPServlet Request.getInputStream()始终接收空白行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆