HTTPServlet Request.getInputStream()始终接收空行

时间:2011-03-03 00:25:47

标签: java servlets file-upload httpwebrequest

客户:

    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,因此似乎没有连接错误。

任何?

2 个答案:

答案 0 :(得分:6)

看起来你的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);

}

答案 1 :(得分:0)

替换以下行

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

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

希望它有效。