zoukankan      html  css  js  c++  java
  • Undertow 作为简单的web文件服务器使用

    Undertow 是基于java nio的web服务器,应用比较广泛,内置提供的PathResourceManager,可以用来直接访问文件系统;如果你有文件需要对外提供访问,除了ftp,nginx等,undertow 也是一个不错的选择,作为java开发,服务搭建非常简便

    服务搭建

    创建一个maven quick-start 项目,并在pom中引入undertow,参考pom配置:

    <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.iflytek</groupId>
    	<artifactId>fileserver</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>fileserver</name>
    	<url>http://maven.apache.org</url>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    	</properties>
    
    	<dependencies>
    		<dependency>
    			<groupId>io.undertow</groupId>
    			<artifactId>undertow-core</artifactId>
    			<version>2.0.22.Final</version>
    		</dependency>
    	</dependencies>
    </project>
    
    

    以下是我的项目结构:

    其中FileServer 代码如下:

    package com.iflytek.fileserver;
    
    import java.io.File;
    
    import io.undertow.Handlers;
    import io.undertow.Undertow;
    import io.undertow.server.handlers.resource.PathResourceManager;
    
    public class FileServer {
        public static void main(String[] args) {
            File file = new File("/");
            Undertow server = Undertow.builder().addHttpListener(8080, "localhost")
                    .setHandler(Handlers.resource(new PathResourceManager(file.toPath(), 100))
                            .setDirectoryListingEnabled(true))
                    .build();
            server.start();
        }
    }
    
    

    好了!运行main函数,打开浏览器访问 http://localhost:8080

    简单的几行代码,搞定!

  • 相关阅读:
    golang 相关
    ES root用户启动失败can not run elasticsearch as root
    基于 Flink CDC + Hudi 湖仓一体方案实践
    数据平台上云
    多云趋势
    数果实时数仓探索
    宽表的设计
    数仓指标体系
    Hudi在医疗大数据的应用
    Hudi on Flink上手使用总结
  • 原文地址:https://www.cnblogs.com/ljgeng/p/11239345.html
Copyright © 2011-2022 走看看