从 HttpServletRequest 读取二进制数据
Reading binary data from HttpServletRequest
使用 Jetty,我将字节发送到 URL http://localhost:8080/input/
,就像这样 -
public static void sampleBytesRequest (String url)
{
try
{
HttpClient client = new HttpClient();
client.start();
client.newRequest(url)
.content(new InputStreamContentProvider(new ByteArrayInputStream("batman".getBytes())))
.send();
}
catch (Exception e) { e.printStackTrace(); }
}
我的服务器(也是 Jetty)有这样的处理程序 -
public final class JettyHandler extends AbstractHandler implements JettyConstants, LqsConstants
{
@Override
public void handle (String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType(UTF_ENCODING);
String requestBody = null;
try { requestBody = baseRequest.getReader().readLine(); }
catch (IOException e) { e.printStackTrace(); }
System.out.println(new String(IOUtils.toByteArray(request.getInputStream())));
}
}
如您所见,我正在尝试从二进制数据重新创建原始字符串并将其打印到标准输出。
但是,如果我在处理程序的打印语句处设置断点,当请求到达该行时,服务器似乎突然跳过了它。
我做错了什么?如何获取我发送的二进制数据并重新创建字符串?
谢谢!
原来问题出在我的客户身上。
而不是
client.newRequest(url)
.content(new InputStreamContentProvider(new ByteArrayInputStream("batman".getBytes())))
.send();
正确的做法是-
client.newRequest(url)
.content(new BytesContentProvider("batman".getBytes()), "text/plain")
.send();
使用 Jetty,我将字节发送到 URL http://localhost:8080/input/
,就像这样 -
public static void sampleBytesRequest (String url)
{
try
{
HttpClient client = new HttpClient();
client.start();
client.newRequest(url)
.content(new InputStreamContentProvider(new ByteArrayInputStream("batman".getBytes())))
.send();
}
catch (Exception e) { e.printStackTrace(); }
}
我的服务器(也是 Jetty)有这样的处理程序 -
public final class JettyHandler extends AbstractHandler implements JettyConstants, LqsConstants
{
@Override
public void handle (String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType(UTF_ENCODING);
String requestBody = null;
try { requestBody = baseRequest.getReader().readLine(); }
catch (IOException e) { e.printStackTrace(); }
System.out.println(new String(IOUtils.toByteArray(request.getInputStream())));
}
}
如您所见,我正在尝试从二进制数据重新创建原始字符串并将其打印到标准输出。
但是,如果我在处理程序的打印语句处设置断点,当请求到达该行时,服务器似乎突然跳过了它。
我做错了什么?如何获取我发送的二进制数据并重新创建字符串?
谢谢!
原来问题出在我的客户身上。
而不是
client.newRequest(url)
.content(new InputStreamContentProvider(new ByteArrayInputStream("batman".getBytes())))
.send();
正确的做法是-
client.newRequest(url)
.content(new BytesContentProvider("batman".getBytes()), "text/plain")
.send();