公司内部传递文件受内部聊天软件限制只能传送 500M 的文件
如果想传更大的文件怎么办?
解决问题的方法当时不止这一种,我们选择这一种:搭建本地的临时文件服务器
学习并使用 java 异步并发的一个工具或框架
依赖如下
<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());
}
知道这个返回的结果吗?
netty 权威指南第二版