2015-07-03 16 views
0

我已經寫了一個服務器上傳文件在Netty中,但我沒有成功實現相當於java中的response.getOutputStream()。 我該怎麼辦,知道我有處置ChannelHandlerContext對象和DefaultFullHttpRequest對象。Netty相當於getOutputStream()

這裏是我的代碼:

@Override 
public void channelRead0(final ChannelHandlerContext chctx, final DefaultFullHttpRequest object) 
    throws Exception { 
    // Retrieving the content 
    ByteBuf data = object.content(); 

    if (HttpMethod.POST.equals(object.getMethod())) { 
     /* while (data.isReadable()) { 
       output.write(data.readByte()); 
     }*/ 
     int dataLen = data.readableBytes(); 
     ByteBuffer nioData; 
     if (data.nioBufferCount() != -1) { 
      nioData = data.nioBuffer(); 
     } else { 
      nioData = ByteBuffer.allocate(dataLen); 
      data.getBytes(data.readerIndex(), nioData); 
      nioData.flip(); // The inputstream 
     } 
     /* FileChannel chan = outputStream.getChannel(); 
      chan.write(data); 
      chan.transferFrom(chctx.write,0,dataLen);*/ 
     // chctx.write(data); 
    } 
} 

回答

0

你可以使用方法ByteBuf的readBytes(OutputStream out,int length)寫在HTTP請求到一個FileOutputStream登頂數據:

... 
OutputStream out = new FileOutputStream("someFile"); 
nioData.readBytes(out, nioData.readableBytes()); 
...