通用技术 netty 学习并搭建文件服务器

bauul · 2017年08月20日 · 最后由 测试小书童 回复于 2017年08月22日 · 1123 次阅读

缘由

公司内部传递文件受内部聊天软件限制只能传送 500M 的文件
如果想传更大的文件怎么办?

解决思路

解决问题的方法当时不止这一种,我们选择这一种:搭建本地的临时文件服务器

为什么使用 netty

学习并使用 java 异步并发的一个工具或框架

新建一个 maven 工程

依赖如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.carl</groupId>
    <artifactId>chen</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>chen</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId> <!-- Use 'netty-all' for 4.0 or above -->
            <version>4.1.14.Final</version>
            <exclusions>
                <exclusion>
                    <groupId>io.netty</groupId>
                    <artifactId>netty-tcnative</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-example</artifactId>
            <version>4.1.14.Final</version>
            <exclusions>
                <exclusion>
                    <groupId>io.netty</groupId>
                    <artifactId>netty-tcnative</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>


    <build>  
    <plugins>  

        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-assembly-plugin</artifactId>  
            <version>3.1.0</version>  
            <configuration>  
                <archive>  
                    <manifest>  
                        <mainClass>com.carl.fileserver.HttpStaticFileServer</mainClass>  
                    </manifest>  
                </archive>  
                <descriptorRefs>  
                    <descriptorRef>jar-with-dependencies</descriptorRef>  
                </descriptorRefs>  
            </configuration>  
        </plugin>  

    </plugins>  
</build>  
</project>

这里我们引入了 example 的包,可以学习一些官方的例子

复制官方的例子并作一点修改

复制这个包:io.netty.example.http.file 中的三个类到自己新建的工程中

修改目标

在执行时,可以指定目录
通过修改 main 方法,将我们指定的目录一步一步的通过构造函数传到 Handler 类中,然后在 Handler 类中的 sanitizeUri 方法返回一个绝对路径

static String rootPath = null;

    public static void main(String[] args) throws Exception {
        if (args.length == 1 && new File(args[0]).exists()) {
            rootPath = args[0];
        } else {
            rootPath = SystemPropertyUtil.get("user.dir");
        }
……

b.group(bossGroup, workerGroup)
             .channel(NioServerSocketChannel.class)
             .handler(new LoggingHandler(LogLevel.INFO))
             .childHandler(new HttpStaticFileServerInitializer(sslCtx, rootPath));

打包

mvn package assembly:single
通过 mvn 打成一个单独的可运行的 jar 包:chen-0.0.1-SNAPSHOT-jar-with-dependencies

运行

无参运行,指定当前目录为根目录:java -jar E:\ABC\FileServer\libs\chen-0.0.1-SNAPSHOT-jar-with-dependencies.jar
带参运行,指定参数目录为根目录:java -jar E:\ABC\FileServer\libs\chen-0.0.1-SNAPSHOT-jar-with-dependencies.jar e:\abc
可以再写一个 bat 和 sh 文件,这样调用就更方便一些了
这样再需要传什么超大文件什么的,就直接起个临时服务,让对方下就好了,等他下好了,再关掉

发现的问题

public static void main(String[] args) {
    System.out.println(new File("e:\\").isHidden());
}

知道这个返回的结果吗?

参考书籍(详细的代码就不抄书了,请看书 10.2 节,不过书有一点老,结合 example 一起看)

netty 权威指南第二版

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 2 条回复 时间 点赞

appium 中 uiautomator2.0 server 即是使用 netty 搭建的异步的 http 的服务😀

很早之前就关注过 netty,很强大

需要 登录 後方可回應,如果你還沒有帳號按這裡 注册