日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

文件服務(wù)器

實(shí)現(xiàn)一個(gè)可以展示指定用戶輸入的文件路徑,返回對(duì)應(yīng)文件內(nèi)容的服務(wù)器。

Netty 實(shí)戰(zhàn):如何實(shí)現(xiàn)文件服務(wù)器?

 

實(shí)例代碼

服務(wù)端

public class FileServer {

    public static void main(String[] args) {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            ChannelFuture channelFuture = serverBootstrap.group(bossGroup, workerGroup)
                    .channel(NIOServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            // 編碼 String
                            ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8))
                                    // 按照行進(jìn)行解碼
                                    .addLast(new LineBasedFrameDecoder(1024))
                                    // String 解碼
                                    .addLast(new StringDecoder(CharsetUtil.UTF_8))
                                    // 大數(shù)據(jù)流的處理
                                    .addLast(new ChunkedWriteHandler())
                                    .addLast(new FileServerHandler());

                        }
                    })
                    .bind(8889)
                    .syncUninterruptibly();
            channelFuture.channel().closeFuture().syncUninterruptibly();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

}

FileServerHandler.JAVA

針對(duì)文件服務(wù)器的處理,實(shí)現(xiàn)如下:

import java.io.RandomaccessFile;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.stream.ChunkedFile;

public class FileServerHandler extends SimpleChannelInboundHandler<String> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // 提醒客戶端輸入文件路徑
        ctx.writeAndFlush("HELLO: Type the path of the file to retrieve.n");
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        // 只讀方式打開(kāi)文件
        try(RandomAccessFile file = new RandomAccessFile(msg, "r")) {
            long length = file.length();
            ctx.write("OK: " + length + 'n');

            ctx.write(new ChunkedFile(file));
            ctx.writeAndFlush("n");
        }
    }

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

整體比較簡(jiǎn)單,exceptionCaught 為異常時(shí)的處理。

channelActive() 為客戶端連接時(shí),服務(wù)端返回客戶端的提示。

channelRead0() 為服務(wù)端對(duì)于客戶端的反饋,就是通過(guò)客戶端輸入的文件路徑,返回文件內(nèi)容。

測(cè)試驗(yàn)證

我們直接使用本地的 telnet

  • 打開(kāi)命令行

輸入 telnet localhost 8889

192:~ houbinbin$ telnet localhost 8889
Trying ::1...
Connected to localhost.
Escape character is '^]'.
HELLO: Type the path of the file to retrieve.
  • 輸入文件路徑
/Users/houbinbin/code/_github/netty-learn/netty-learn-four/src/main/java/com/github/houbb/netty/learn/four/file/FileServer.java

反饋如下:

就是把 FileServer.java 這個(gè)文件內(nèi)容全部返回回來(lái)了。

OK: 2387
/*
 * Copyright (c)  2019. houbinbin Inc.
 * netty-learn All rights reserved.
 */

package com.github.houbb.netty.learn.four.file;

....... 內(nèi)容省略

/**
 * <p> </p>
 *
 * <pre> Created: 2019/9/21 11:49 PM  </pre>
 * <pre> Project: netty-learn  </pre>
 *
 * @author houbinbin
 */
public class FileServer {

    public static void main(String[] args) {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        .... 內(nèi)容省略
    }

}

Connection closed by foreign host.

分享到:
標(biāo)簽:文件服務(wù)器
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定