利用Netty中提供的HttpChunk简单实现文件传输

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

public class HttpClient {

    private ClientBootstrap bootstrap;
    private String host="localhost";
    private Channel channel;
    private boolean futureSuccess;
    private int port=8080;

    public HttpClient() {
    }

    public ChannelFuture connect() {
        bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(),Executors
                .newCachedThreadPool()));
        HttpResponseHandler clientHandler = new HttpResponseHandler();
        bootstrap.setPipelineFactory(new HttpClientPipelineFactory(clientHandler));

        bootstrap.setOption("tcpNoDelay",true);
        bootstrap.setOption("keepAlive",true);

        return bootstrap.connect(new InetSocketAddress(host,port));
    }

    public boolean checkFutureState(ChannelFuture channelFuture) {
        // Wait until the connection attempt succeeds or fails.
        channel = channelFuture.awaitUninterruptibly().getChannel();
        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture connectFuture) throws Exception {
                if (!connectFuture.isSuccess()) {
                    connectFuture.getCause().printStackTrace();
                    // connectFuture.getChannel().close();
                    // bootstrap.releaseExternalResources();
                    futureSuccess = false;
                } else {
                    futureSuccess = true;
                }
            }
        });
        return futureSuccess;
    }

    public ChannelFuture write(HttpRequest request) {
        return channel.write(request);
    }

    public void Close() {
        // Close the connection. Make sure the close operation ends because
        // all I/O operations are asynchronous in Netty.
        channel.close().awaitUninterruptibly();
        // Shut down all thread pools to exit.
        bootstrap.releaseExternalResources();
    }
}

public class HttpClientPipelineFactory implements ChannelPipelineFactory {  
    private final HttpResponseHandler handler;  

    public HttpClientPipelineFactory(HttpResponseHandler handler) {  
        this.handler = handler;  
    }  

    public ChannelPipeline getPipeline() throws Exception {  
        ChannelPipeline pipeline = pipeline();  

        pipeline.addLast("decoder",new HttpResponseDecoder());  
        //pipeline.addLast("aggregator",new HttpChunkAggregator(6048576));  
        pipeline.addLast("encoder",new HttpRequestEncoder());  
        pipeline.addLast("chunkedWriter",new ChunkedWriteHandler());  
        pipeline.addLast("handler",handler);  

        return pipeline;  
    }  
}  

@ChannelPipelineCoverage("one")  
public class HttpResponseHandler extends SimpleChannelUpstreamHandler {  
    private volatile boolean readingChunks;  
    private File downloadFile;  
    private FileOutputStream fOutputStream = null;  

    @Override  
    public void messageReceived(ChannelHandlerContext ctx,MessageEvent e) throws Exception {  
        if (e.getMessage() instanceof HttpResponse) {  
            DefaultHttpResponse httpResponse = (DefaultHttpResponse) e.getMessage();  
            String fileName = httpResponse.getHeader("Content-Disposition").substring(20);  
            downloadFile = new File(System.getProperty("user.dir") + File.separator + "download" + fileName);  
            readingChunks = httpResponse.isChunked();  
        } else {  
            HttpChunk httpChunk = (HttpChunk) e.getMessage();  
            if (!httpChunk.isLast()) {  
                ChannelBuffer buffer = httpChunk.getContent();  
                if (fOutputStream == null) {  
                    fOutputStream = new FileOutputStream(downloadFile);  
                }  
                while (buffer.readable()) {  
                    byte[] dst = new byte[buffer.readableBytes()];  
                    buffer.readBytes(dst);  
                    fOutputStream.write(dst);  
                }  
            } else {  
                readingChunks = false;  
            }  
            fOutputStream.flush();  
        }  
        if (!readingChunks) {  
            fOutputStream.close();  
        }  
    }  

    @Override  
    public void exceptionCaught(ChannelHandlerContext ctx,ExceptionEvent e) throws Exception {  
        System.out.println(e.getCause());  
    }  
}  

public class ClientMain {  
    public static void main(String[] args) {  
        HttpClient httpClient=new HttpClient();  
        ChannelFuture connectFuture=httpClient.connect();  
        if (httpClient.checkFutureState(connectFuture)) {  
            System.out.println("connect ok");  
            HttpRequest request=new DefaultHttpRequest(HttpVersion.HTTP_1_1,HttpMethod.GET,"thunder.zip");  
//          HttpRequest request=new DefaultHttpRequest(HttpVersion.HTTP_1_1,"thunder.java");  
            ChannelFuture writeFuture= httpClient.write(request);  
            if (httpClient.checkFutureState(writeFuture)) {  
                System.out.println("write ok");  
            }  
        }  
    }  
}  

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

相关文章

Netty实现httpserver简单示例 3个Java类实现最基本的接收请求...
Java NIO系列1 概观 Java NIO。中间的N你既可以理解为(new)...
关键字:使用Netty实现HTTP服务器,使用Netty实现httpserver...
netty心跳机制示例,使用Netty实现心跳机制,使用netty4,Id...
关键字:Netty开发redis客户端,Netty发送redis命令,netty解...
前提 最近一直在看Netty相关的内容,也在编写一个轻量级的RP...