2015-01-16 65 views
1

我使用httpcomponenets nio服務器來處理髮布請求文件上傳。如何解析httpcomponents nio服務器處理程序中的發佈請求?

下面是示例代碼。我有數據字節數組中的完整數據,包括由邊界分隔的參數,上傳的文件等。是否有解析器實用程序來解析數據並獲取參數?像的request.getParameter( 「參數1」),request.getFile()等等

public static void main(String[] args) throws Exception { 
     int port = 8280; 

     // Create HTTP protocol processing chain 
     HttpProcessor httpproc = HttpProcessorBuilder.create() 
      .add(new ResponseDate()) 
      .add(new ResponseServer("Test/1.1")) 
      .add(new ResponseContent()) 
      .add(new ResponseConnControl()).build(); 
     // Create request handler registry 
     UriHttpAsyncRequestHandlerMapper reqistry = new UriHttpAsyncRequestHandlerMapper(); 
     // Register the default handler for all URIs 
     reqistry.register("/test*", new RequestHandler()); 
     // Create server-side HTTP protocol handler 
     HttpAsyncService protocolHandler = new HttpAsyncService(httpproc, reqistry) { 

      @Override 
      public void connected(final NHttpServerConnection conn) { 
       System.out.println(conn + ": connection open"); 
       super.connected(conn); 
      } 

      @Override 
      public void closed(final NHttpServerConnection conn) { 
       System.out.println(conn + ": connection closed"); 
       super.closed(conn); 
      } 

     }; 
     // Create HTTP connection factory 
     NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory; 

      connFactory = new DefaultNHttpServerConnectionFactory(
       ConnectionConfig.DEFAULT); 
     // Create server-side I/O event dispatch 
     IOEventDispatch ioEventDispatch = new DefaultHttpServerIODispatch(protocolHandler, connFactory); 
     // Set I/O reactor defaults 
     IOReactorConfig config = IOReactorConfig.custom() 
      .setIoThreadCount(1) 
      .setSoTimeout(3000) 
      .setConnectTimeout(3000) 
      .build(); 
     // Create server-side I/O reactor 
     ListeningIOReactor ioReactor = new DefaultListeningIOReactor(config); 
     try { 
      // Listen of the given port 
      ioReactor.listen(new InetSocketAddress(port)); 
      // Ready to go! 
      ioReactor.execute(ioEventDispatch); 
     } catch (InterruptedIOException ex) { 
      System.err.println("Interrupted"); 
     } catch (IOException e) { 
      System.err.println("I/O error: " + e.getMessage()); 
     } 
     System.out.println("Shutdown"); 
    } 
public static class RequestHandler implements HttpAsyncRequestHandler<HttpRequest> { 
    public void handleInternal(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException { 

     HttpEntity entity = null; 
     if (httpRequest instanceof HttpEntityEnclosingRequest) 
      entity = ((HttpEntityEnclosingRequest)httpRequest).getEntity(); 

     byte[] data; 
     if (entity == null) { 
      data = new byte [0]; 
     } else { 
      data = EntityUtils.toByteArray(entity); 
     } 

     System.out.println(new String(data)); 

     httpResponse.setEntity(new StringEntity("success response")); 
    } 

    @Override public HttpAsyncRequestConsumer<HttpRequest> processRequest(HttpRequest request, HttpContext context) throws HttpException, IOException { 
     return new BasicAsyncRequestConsumer(); 
    } 

    @Override 
    public void handle(HttpRequest request, HttpAsyncExchange httpExchange, HttpContext context) throws HttpException, IOException { 
     HttpResponse response = httpExchange.getResponse(); 
     handleInternal(request, response, context); 
     httpExchange.submitResponse(new BasicAsyncResponseProducer(response)); 

    } 
} 
+0

請求實體內容的格式是什麼? URL編碼形式?多部分表單(MIME)? – oleg

+0

其多部分/表單數據。我正嘗試將文件上傳到服務器。 –

回答

1

MIME內容解析(以及處理的任何類型的內容)的東西是超出範圍爲Apache HttpComponents。請考慮使用Apache Mime4J

+0

以netty結束 –

相關問題