Android Netty FileRegion传输失败

Android Netty FileRegion传输失败,android,netty,Android,Netty,我正在开发一个Netty HTTP服务器,它只需通过HTTP尽可能快速高效地发送本地文件 现在传输提前停止,错误消息如下所示: $ curl http://192.168.56.101:32324/dl/%2Fstorage%2Femulated%2F0%2FMovies%2Fstartrek-tlr3-german_h720p.mov > /dev/null % Total % Received % Xferd Average Speed Time Time

我正在开发一个Netty HTTP服务器,它只需通过HTTP尽可能快速高效地发送本地文件

现在传输提前停止,错误消息如下所示:

$ curl http://192.168.56.101:32324/dl/%2Fstorage%2Femulated%2F0%2FMovies%2Fstartrek-tlr3-german_h720p.mov > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (18) transfer closed with 90831821 bytes remaining to read

$ curl http://192.168.56.101:32324/dl/%2Fstorage%2Femulated%2F0%2FMovies%2Fstartrek-tlr3-german_h720p.mov > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (18) transfer closed with 90833209 bytes remaining to read
为此,我将Netty包括在内,如下所示:

$ curl http://192.168.56.101:32324/dl/%2Fstorage%2Femulated%2F0%2FMovies%2Fstartrek-tlr3-german_h720p.mov > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (18) transfer closed with 90831821 bytes remaining to read

$ curl http://192.168.56.101:32324/dl/%2Fstorage%2Femulated%2F0%2FMovies%2Fstartrek-tlr3-german_h720p.mov > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (18) transfer closed with 90833209 bytes remaining to read
构建.渐变

// Netty & Friends.
compile group: 'io.netty', name: 'netty-all', version: '4.1.0.Beta4'
compile group: 'tv.cntt', name: 'jauter', version: '1.7'
compile 'javax.activation:activation:1.1.1'
compile 'org.javassist:javassist:3.19.0-GA'
final class MediaLinkServerInitializer extends ChannelInitializer<SocketChannel> {

    private final Handler handler;

    public MediaLinkServerInitializer(Context context) {
        final Router router = new Router()
                .GET("/dl/:path", StaticFileHandler.class)
                .HEAD("/dl/:path", StaticFileHandler.class);

        handler = new Handler(router);
    }

    @Override
    public void initChannel(SocketChannel ch) {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new HttpServerCodec());
        pipeline.addLast(handler.name(), handler);
    }
}
public class StaticFileHandler extends SimpleChannelInboundHandler<Routed> {
    @Override
    public void channelRead0(final ChannelHandlerContext ctx, final Routed request) throws Exception {
        File file = ... // get from URL, check etc.
        RandomAccessFile raf;
        try {
            raf = new RandomAccessFile(file, "r");
        } catch (FileNotFoundException ignore) {
            sendError(ctx, NOT_FOUND);
            return;
        }

        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
        setContentLength(response, fileLength);
        setContentTypeHeader(response, file);
        setDateAndCacheHeaders(response, file);
        setKeepAlive(response, isKeepAlive(request.request()));

        // Write the initial line and the header.
        ctx.write(response);

        ChannelProgressiveFuture future = (ChannelProgressiveFuture) ctx.writeAndFlush(
            new DefaultFileRegion(raf.getChannel(), 0, fileLength), ctx.newProgressivePromise());

        future.addListener(new ChannelProgressiveFutureListener() {
            @Override
            public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) throws Exception {

            }

            @Override
            public void operationComplete(ChannelProgressiveFuture future) throws Exception {
                log.log(Level.FINEST, "Transfer complete: {0}", future.channel());
            }
        });
        ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
    }
}
MediaLinkServerInitializer.java

// Netty & Friends.
compile group: 'io.netty', name: 'netty-all', version: '4.1.0.Beta4'
compile group: 'tv.cntt', name: 'jauter', version: '1.7'
compile 'javax.activation:activation:1.1.1'
compile 'org.javassist:javassist:3.19.0-GA'
final class MediaLinkServerInitializer extends ChannelInitializer<SocketChannel> {

    private final Handler handler;

    public MediaLinkServerInitializer(Context context) {
        final Router router = new Router()
                .GET("/dl/:path", StaticFileHandler.class)
                .HEAD("/dl/:path", StaticFileHandler.class);

        handler = new Handler(router);
    }

    @Override
    public void initChannel(SocketChannel ch) {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new HttpServerCodec());
        pipeline.addLast(handler.name(), handler);
    }
}
public class StaticFileHandler extends SimpleChannelInboundHandler<Routed> {
    @Override
    public void channelRead0(final ChannelHandlerContext ctx, final Routed request) throws Exception {
        File file = ... // get from URL, check etc.
        RandomAccessFile raf;
        try {
            raf = new RandomAccessFile(file, "r");
        } catch (FileNotFoundException ignore) {
            sendError(ctx, NOT_FOUND);
            return;
        }

        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
        setContentLength(response, fileLength);
        setContentTypeHeader(response, file);
        setDateAndCacheHeaders(response, file);
        setKeepAlive(response, isKeepAlive(request.request()));

        // Write the initial line and the header.
        ctx.write(response);

        ChannelProgressiveFuture future = (ChannelProgressiveFuture) ctx.writeAndFlush(
            new DefaultFileRegion(raf.getChannel(), 0, fileLength), ctx.newProgressivePromise());

        future.addListener(new ChannelProgressiveFutureListener() {
            @Override
            public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) throws Exception {

            }

            @Override
            public void operationComplete(ChannelProgressiveFuture future) throws Exception {
                log.log(Level.FINEST, "Transfer complete: {0}", future.channel());
            }
        });
        ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
    }
}
StaticFileHandler.java

// Netty & Friends.
compile group: 'io.netty', name: 'netty-all', version: '4.1.0.Beta4'
compile group: 'tv.cntt', name: 'jauter', version: '1.7'
compile 'javax.activation:activation:1.1.1'
compile 'org.javassist:javassist:3.19.0-GA'
final class MediaLinkServerInitializer extends ChannelInitializer<SocketChannel> {

    private final Handler handler;

    public MediaLinkServerInitializer(Context context) {
        final Router router = new Router()
                .GET("/dl/:path", StaticFileHandler.class)
                .HEAD("/dl/:path", StaticFileHandler.class);

        handler = new Handler(router);
    }

    @Override
    public void initChannel(SocketChannel ch) {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new HttpServerCodec());
        pipeline.addLast(handler.name(), handler);
    }
}
public class StaticFileHandler extends SimpleChannelInboundHandler<Routed> {
    @Override
    public void channelRead0(final ChannelHandlerContext ctx, final Routed request) throws Exception {
        File file = ... // get from URL, check etc.
        RandomAccessFile raf;
        try {
            raf = new RandomAccessFile(file, "r");
        } catch (FileNotFoundException ignore) {
            sendError(ctx, NOT_FOUND);
            return;
        }

        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
        setContentLength(response, fileLength);
        setContentTypeHeader(response, file);
        setDateAndCacheHeaders(response, file);
        setKeepAlive(response, isKeepAlive(request.request()));

        // Write the initial line and the header.
        ctx.write(response);

        ChannelProgressiveFuture future = (ChannelProgressiveFuture) ctx.writeAndFlush(
            new DefaultFileRegion(raf.getChannel(), 0, fileLength), ctx.newProgressivePromise());

        future.addListener(new ChannelProgressiveFutureListener() {
            @Override
            public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) throws Exception {

            }

            @Override
            public void operationComplete(ChannelProgressiveFuture future) throws Exception {
                log.log(Level.FINEST, "Transfer complete: {0}", future.channel());
            }
        });
        ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
    }
}
对于那些早期失败的转移,会有什么问题