我正在尝试读取请求InputStream
,但已到达流的末尾。
唯一的类(servlet)是:
public class TestServlet extends HttpServlet
{
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
InputStream clientIn = request.getInputStream();
OutputStream clientOut = response.getOutputStream();
byte[] buffer = new byte[4096];
int n;
try
{
while ((n = clientIn.read(buffer)) != -1) // ---------> Here, n is -1
{
System.out.println(new String(buffer,0,n));
clientOut.write(("Ok = " + n).getBytes());
}
}
catch (Exception e)
{
System.err.println(e);
}
}
}
没有任何其他类(例如过滤器、侦听器或其他 servlet)
客户端代码是:
公共类主 { 公共静态 void main(String[] args) 抛出 IOException、InterruptedException { 字符串 postCommand = "POST / HTTP/1.1\r\n" + "主机:本地主机\r\n" + "内容类型:二进制/八位字节流\r\n" + "连接:保持活动\r\n\r\n" + “名称1=值1&名称2=值2”; Socket socket = new Socket("localhost", 8080); InputStream serverIn = socket.getInputStream(); 输出流服务器输出 = socket.getOutputStream(); serverOut.write(postCommand.getBytes()); 整数 n = 0,计数 = 1; 字节[]缓冲区=新字节[4096]; 做 { 如果(n!= 0) System.out.println(new String(buffer, 0, n)); serverOut.write(("foo" + count).getBytes()); } while ((n = serverIn.read(buffer)) != -1 && count++
先感谢您。亲切的问候!