Example usage for io.netty.handler.stream ChunkedFile length

List of usage examples for io.netty.handler.stream ChunkedFile length

Introduction

In this page you can find the example usage for io.netty.handler.stream ChunkedFile length.

Prototype

@Override
    public long length() 

Source Link

Usage

From source file:com.yeetor.server.HttpServer.java

License:Open Source License

public void doFileRequest(ChannelHandlerContext ctx, HttpRequest request, HttpResponse response) {
    String location = request.uri().substring(1);
    if (location.indexOf("?") != -1) {
        location = location.substring(0, location.indexOf("?"));
    }/*from   w ww.  ja  v a 2 s  .  c o  m*/
    if (location.equals("")) {
        location = INDEX_FILE;
    }

    File localFile = new File(Constant.getResourceDir(), "web" + File.separator + location);

    if (!localFile.exists() || localFile.isHidden()) {
        writeErrorHttpResponse(ctx, request, response, HttpResponseStatus.NOT_FOUND);
        return;
    }

    try {
        RandomAccessFile raf = new RandomAccessFile(localFile, "r");
        ChunkedFile chunkedFile = new ChunkedFile(raf, 0, localFile.length(), 8192);
        long fileSize = chunkedFile.length();

        response.headers().add(CONTENT_LENGTH, localFile.length());

        MimetypesFileTypeMap mimetypesFileTypeMap = new MimetypesFileTypeMap();
        response.headers().add(CONTENT_TYPE, mimetypesFileTypeMap.getContentType(localFile.getPath()));

        writeResponse(ctx, request, response, chunkedFile);

    } catch (IOException e) {
        writeErrorHttpResponse(ctx, request, response, HttpResponseStatus.INTERNAL_SERVER_ERROR);
        return;
    }
}