title: How can I handle multipart form data in Java NIO Htttp Server
body: <p>I have implemented a Non Blocking Htttp Server by using Java NIO. It works fine for x-www-form-urlencoded POST requests. But when i try it for a HTTP multipart request with large file, it is not working. In that situation Server unable to make response to http client. This is my source code for the NIO Server.</p>
  
  <pre><code>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&lt;SelectionKey&gt; selectionKeys = selector.selectedKeys();
                    Iterator&lt;SelectionKey&gt; 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 &gt; 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());
          }
  
      }
  
   }
  </code></pre>
  
  <p>Is there any other way to handle HTTP multipart request inside Java NIO non blocking server.How can i fix this. Thanks.</p>