com.sheldon.javaPrj.netty.FileClientHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.sheldon.javaPrj.netty.FileClientHandler.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.sheldon.javaPrj.netty;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.stream.ChunkedFile;
import java.io.File;

/**
 *
 * @author xiangnan.wang@ipinyou.com
 */
public class FileClientHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("send file");
        File f = new File("/home/wangxiangnan/Dump20140717.sql");
        ctx.channel().writeAndFlush(new ChunkedFile(f));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

}