FileServerHandler.java 文件源码

java
阅读 27 收藏 0 点赞 0 评论 0

项目:netty-study 作者:
@Override
protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {

    File file = new File(msg);
    if (file.exists()) {
        if (!file.isFile()) {
            ctx.writeAndFlush("not a file :" + file + CR);
            return;
        }
        ctx.write(file + " " + file.length() + CR);

        RandomAccessFile raf = new RandomAccessFile(file, "r");
        FileRegion fileRegion = new DefaultFileRegion(raf.getChannel(), 0, raf.length());

        ctx.write(fileRegion);
        ctx.writeAndFlush(CR);
        raf.close();

    } else {
        ctx.writeAndFlush("file not found: " + file + CR);
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号