当前位置: 首页>>代码示例>>Java>>正文


Java ChannelFutureProgressListener类代码示例

本文整理汇总了Java中org.jboss.netty.channel.ChannelFutureProgressListener的典型用法代码示例。如果您正苦于以下问题:Java ChannelFutureProgressListener类的具体用法?Java ChannelFutureProgressListener怎么用?Java ChannelFutureProgressListener使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


ChannelFutureProgressListener类属于org.jboss.netty.channel包,在下文中一共展示了ChannelFutureProgressListener类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: messageReceived

import org.jboss.netty.channel.ChannelFutureProgressListener; //导入依赖的package包/类
@Override
public void messageReceived( ChannelHandlerContext ctx, MessageEvent e ) throws Exception
{
    HttpRequest request = (HttpRequest)e.getMessage( ) ;
    if( request.getMethod( ) != GET )
    {
        sendError( ctx, FORBIDDEN ) ;
        return ;
    }
    
    final String path = sanitizeUri( request.getUri( ) ) ;
    if( path == null )
    {
        sendError( ctx, FORBIDDEN ) ;
        return ;
    }
    
    File file = new File(path) ;
    if( file.isHidden( ) || !file.exists( ) )
    {
        sendError( ctx, NOT_FOUND ) ;
        return ;
    }
    
    RandomAccessFile raf ;
    try
    {
        raf = new RandomAccessFile( file, "r" ) ;
    }
    catch( FileNotFoundException fnfe )
    {
        sendError( ctx, NOT_FOUND ) ;
        return;
    }
    long fileLength = raf.length( ) ;
    HttpResponse response = new DefaultHttpResponse( HTTP_1_1, OK ) ;
    setContentLength( response, fileLength ) ;
    
    Channel ch = e.getChannel( ) ;
    
    //Escreve a linha inicial do cabe�alho
    ch.write( response ) ;
    
    // Escreve o conte�do
    ChannelFuture writeFuture ;
    if( ch.getPipeline( ).get( SslHandler.class ) != null )
    {
        writeFuture = ch.write( new ChunkedFile( raf, 0, fileLength, 8192 ) ) ;
    }
    else
    {
        final FileRegion region = new DefaultFileRegion( raf.getChannel( ), 0, fileLength ) ;
        writeFuture = ch.write( region ) ;
        writeFuture.addListener( new ChannelFutureProgressListener( )
        {
            @Override
            public void operationComplete( ChannelFuture arg0 ) throws Exception
            {
                region.releaseExternalResources( ) ;
            }
            
            @Override
            public void operationProgressed( ChannelFuture future, long amount, long current,  long total ) throws Exception
            {
                System.out.printf( "%s: %d / %d (+%d)%n", path, current, total, amount );
            }
        }) ;
    }
    
    // Decide se fecha a conex�o ou n�o!!
    if( !isKeepAlive( request ) )
    {
        writeFuture.addListener( ChannelFutureListener.CLOSE ) ;
    }
}
 
开发者ID:wsyssantos,项目名称:BettaServer,代码行数:76,代码来源:BettaUdpFileServerHandler.java


注:本文中的org.jboss.netty.channel.ChannelFutureProgressListener类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。