FileServerHandler.java 文件源码

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

项目:netty-book 作者:
public 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 randomAccessFile = new RandomAccessFile(msg, "r");
    FileRegion region = new DefaultFileRegion(
        randomAccessFile.getChannel(), 0, randomAccessFile.length());
    ctx.write(region);
    ctx.writeAndFlush(CR);
    randomAccessFile.close();
} else {
    ctx.writeAndFlush("File not found: " + file + CR);
}
   }
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号