我使用Java NIO实现了非阻塞Htttp服务器。它适用于x-www-form-urlencoded POST请求。但是,当我尝试使用大文件的HTTP多部分请求时,它无法正常工作。在那种情况下,服务器无法响应http客户端。这是我的NIO服务器的源代码。
public class TCPServer {
public static void main(String[] args) {
TCPServer server = new TCPServer();
server.listen();
}
public void listen() {
try {
Selector selector = Selector.open();
ServerSocketChannel serverSocketChannel = ServerSocketChannel
.open();
InetSocketAddress serverAddress = new InetSocketAddress(8080);
serverSocketChannel.bind(serverAddress);
serverSocketChannel.configureBlocking(false);
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
selector.select();
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
if (key.isAcceptable()) {
SocketChannel clientSocketChannel = serverSocketChannel.accept();
clientSocketChannel.configureBlocking(false);
clientSocketChannel.register(selector,
SelectionKey.OP_READ);
} else if (key.isReadable()) {
SocketChannel clientSocketChannel = null;
try {
clientSocketChannel = (SocketChannel) key.channel();
ByteBuffer clientBuffer = ByteBuffer.allocate(1024);
StringBuilder requestStringBuilder = new StringBuilder();
int bytesRead = clientSocketChannel
.read(clientBuffer);
while (bytesRead > 0) {
clientBuffer.flip();
String result = new String(clientBuffer.array());
requestStringBuilder.append(result);
clientBuffer.compact();
bytesRead = clientSocketChannel.read(clientBuffer);
}
System.out.println("request-----");
System.out
.println(requestStringBuilder.toString());
clientSocketChannel.write(ByteBuffer
.wrap("reply from server".getBytes()));
clientSocketChannel.register(selector,
SelectionKey.OP_WRITE);
} catch (Exception e) {
System.err.println(e.getMessage());
clientSocketChannel.close();
}
} else if (key.isWritable()) {
SocketChannel clientSocketChannel = null;
try {
clientSocketChannel = (SocketChannel) key.channel();
clientSocketChannel.close();
} catch (Exception e) {
System.err.println(e.getMessage());
clientSocketChannel.close();
}
}
iterator.remove();
}
}
}catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
在Java NIO非阻塞服务器中是否还有其他方法可以处理HTTP多部分请求。如何解决这个问题。感谢。
答案 0 :(得分:0)
这与另一个问题类似:Servlet 3.1 - Multipart async processing,但我会在这里回答,因为该解决方案也适用于普通的非阻塞IO。
Synchronoss Technologies最近开源了一个非阻塞的HTTP多部分解析器here。
当您的非阻塞服务器接收数据时,您只需将传入的字节传递给 <p:autoComplete value="#{bean.value}
multiple="true"
completeMethod="#{bean.completeTheme}"
forceSelection="false"/>
。解析器将为您收到的每个部分的代码进行回调。
免责声明:我为Synchronoss Technologies工作。我们为Servlet 3.1写了这个,但它也应该在常规的非阻塞应用程序中有意义地工作,所以希望其他人会发现这个库很有用。