我正在基于Tomcat Servlets和NIO创建服务。在输入时有大的XML请求(~100 MB),通过HTML POST方法发送。我想只流出前8 KiB,之后立即向客户发送响应。
public class A extends HttpServlet {
@Override
protected void service(HttpServletRequest rq, HttpServletResponse rs) {
byte[] buffer = new byte[1024*8];
try {
rq.getInputStream().read(buffer);
rs.setContentType("text/plain");
rs.getOutputStream().write("Some Response".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
当我尝试发送小请求(内容中的几行)时,没有问题,套接字正常工作。
2016-02-01 10:44:52 Http11NioProtocol [DEBUG] Socket: [org.apache.tomcat.util.net.NioEndpoint$KeyAttachment@74f19fed:org.apache.tomcat.util.net.NioChannel@c478210:java.nio.channels.SocketChannel中[连接 local = / 0:0:0:0:0:0:0:1:8080 remote = / 0:0:0:0:0:0:0:1:63943]],状态 在:[OPEN_READ],陈述:[ OPEN ]
但是,如果我尝试发送更大的请求(超过100 MB),则客户端没有响应。
2016-02-01 10:48:42 Http11NioProtocol [DEBUG] Socket: [org.apache.tomcat.util.net.NioEndpoint$KeyAttachment@2b36c88f:org.apache.tomcat.util.net.NioChannel@25f12241:java.nio.channels.SocketChannel中[连接 local = / 0:0:0:0:0:0:0:1:8080 remote = / 0:0:0:0:0:0:0:1:64079]],状态 in:[OPEN_READ],陈述:[已关闭]
2016-02-01 10:48:42 LimitLatch [DEBUG]计数 down [http-nio-8080-exec-3] latch = 1
在我读取整个输入流请求之前,Tomcat不想打开socket(状态输出:CLOSED)。
是否可以在不阅读整个请求的情况下将响应发送到客户端?根据规范,我能够在请求的前8个KiB上找到有趣的我的信息。
答案 0 :(得分:0)
您的代码只读取请求的一部分,
您可以使用此方法将Stream转换为String
private static String convertStreamToString(InputStream is) throws IOException{
int i = 0;
byte[] buff = new byte[1024];
StringBuilder sb = new StringBuilder();
while (i != -1) {
i = is.read(buff);
if(i != -1){
sb.append(new String(buff,0,i));
}
}
return sb.toString();
}
所以,在你的代码中它将是
public class A extends HttpServlet {
@Override
protected void service(HttpServletRequest rq, HttpServletResponse rs) {
try {
String response = convertStreamToString(rq.getInputStream());
rs.getOutputStream().write(response.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
private static String convertStreamToString(InputStream is) throws IOException{
int i = 0;
byte[] buff = new byte[1024];
StringBuilder sb = new StringBuilder();
while (i != -1) {
i = is.read(buff);
if(i != -1){
sb.append(new String(buff,0,i));
}
}
return sb.toString();
}
}
或者您可以使用Scanner直接将您的Stream转换为字符串
Scanner scanner = new Scanner(inputStream);
StringBuilder sb = new StringBuilder();
while (scanner.hasNext()) {
sb.append(scanner.next());
}
所以,在你的代码中它应该是
public class A extends HttpServlet {
@Override
protected void service(HttpServletRequest rq, HttpServletResponse rs) {
try {
Scanner scanner = new Scanner(rq.getInputStream());
StringBuilder sb = new StringBuilder();
while (scanner.hasNext()) {
sb.append(scanner.next());
}
rs.getOutputStream().write(sb.toString().getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
此代码
byte[] buffer = new byte[1024*8];
rq.getInputStream().read(buffer);
只读取1024字节请求并将其存储到缓冲区数组(1024字节),方法read
将返回读取的字节数,因为根本不会读取1024字节,如果请求只发送200字节,824个字节将是无用的。所以,如果请求只发送200个字节
int x = rq.getInputStream().read(buffer);
这个x,将是200, 所以要完全阅读,你需要制作循环
// integer to read
int bytesRead = 0;
// loop until bytesRead is -1 (end of request), -1 means there are no bytes to read, so it will read until there are no bytes to read
while(bytesRead != -1){
bytesRead = rq.getInputStream().read(buffer);
if(bytesRead != -1){
// process the bytes here , but only process bytes from 0 to bytesRead
}
}