Serve un file con Netty - risposta viene troncato da un byte
-
27-10-2019 - |
Domanda
Ho servono a file da attività Android tramite server Netty (immagini, html). I file di testo tale html viene salvata come .mp3 per disabilitare la compressione (Ho bisogno di un InputStream!)
La mia condotta è alla ricerca in questo modo:
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("aggregator", new HttpChunkAggregator(65536));
pipeline.addLast("encoder", new HttpResponseEncoder());
pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
pipeline.addLast("handler", new AssetsServerHandler(context));
Il mio gestore è:
public class AssetsServerHandler extends SimpleChannelUpstreamHandler {
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
// some checks
final FileInputStream is;
final AssetFileDescriptor afd;
try {
afd = assetManager.openFd(path);
is = afd.createInputStream();
} catch(IOException exc) {
sendError(ctx, NOT_FOUND);
return;
}
final long fileLength = afd.getLength();
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
setContentLength(response, fileLength);
final Channel ch = e.getChannel();
final ChannelFuture future;
ch.write(response);
future = ch.write(new ChunkedStream(is));
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
future.getChannel().close();
}
});
if (!isKeepAlive(request)) {
future.addListener(ChannelFutureListener.CLOSE);
}
}
// other stuff
}
Con questo gestore ho il mio resposes troncati da almeno un byte. Se cambio ChunkedStream
a ChunkedNioFile
(e quindi utilizzare un is.getChannel()
invece di is
come costruttore ad esso) - tutto funziona perfettamente.
Per favore, aiutami a capire ciò che è sbagliato con ChunkedStream.
Soluzione
Il codice è giusto per me. Fa il FileInputStream tornata di AssetFileDescriptor contiene "tutti i byte"? Si potrebbe verificare questo con una prova di unità. Se non ci sono bug in esso allora il suo un bug in Netty. Mi fanno un largo uso di ChunkInputStream e mai avuto un problema ancora, ma forse in realtà dipende dalla natura del InputStream.
Sarebbe bello se si potesse scrivere un banco di prova e aprire un problema a github di Netty.