2014-09-17 1 views
0

netty에 익숙하지 않으며 서버에서 클라이언트로 청크 파일을 전송하려고합니다. 청크를 보내면 잘 작동합니다. 문제는 수신 된 청크를 처리하고 파일에 기록하는 방법에 있습니다. 두 가지 방법 모두 직접 버퍼 오류를 일으켰습니다.Netty : ChunkedFile에서 수신 된 청크를 처리하는 방법

도움을 주시면 감사하겠습니다.

감사합니다.

@Override 
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { 

     System.out.println(in.toString()); 


     //METHOD 1: write to file 
     FileOutputStream fos = new FileOutputStream("c:\\test.txt"); 
     fos.write(in.array()); 


     //METHOD 2: getting desperate 
     //InputStream inputStream = new ByteBufInputStream(in); 
     //ChunkedStream chkStream = new ChunkedStream(inputStream);    
     //outputStream.write((chkStream.readChunk(ctx).readBytes(in, 0)).array()); 

     //if(chkStream.isEndOfInput()){ 
     // outputStream.close(); 
     // inputStream.close(); 
     // chkStream.close(); 
     //} 


     return; 

    } 

    out.add(in.toString(charset)); 

} 

답변

0

이되는 FileChannel을 사용

ByteBuf in = ...; 
ByteBuffer nioBuffer = in.nioBuffer(); 
FileOutputStream fos = new FileOutputStream("c:\\test.txt"); 
FileChannel channel = fos.getChannel(); 
while (nioBuffer.hasRemaining()) { 
    channel.write(nioBuffer); 
} 
channel.close(); 
fos.close(); 
+0

당신에게 어떤 선생님 감사합니다. 그건 내 바보 같았 어! –

관련 문제