首页 > 代码库 > 使用嵌入式jetty实现文件服务器

使用嵌入式jetty实现文件服务器

pom.xml

<?xml version="1.0" encoding="UTF-8"?><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.nihaorz</groupId>    <artifactId>jetty-file-server</artifactId>    <version>1.0-SNAPSHOT</version>    <dependencies>        <dependency>            <groupId>org.eclipse.jetty</groupId>            <artifactId>jetty-server</artifactId>            <version>8.1.22.v20160922</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-assembly-plugin</artifactId>                <version>3.0.0</version>                <configuration>                    <archive>                        <manifest>                            <mainClass>com.nihaorz.app.AppStart</mainClass>                        </manifest>                    </archive>                    <descriptorRefs>                        <descriptorRef>jar-with-dependencies</descriptorRef>                    </descriptorRefs>                </configuration>            </plugin>        </plugins>    </build></project>

 

AppStart.java

package com.nihaorz.app;import org.eclipse.jetty.server.Handler;import org.eclipse.jetty.server.Server;import org.eclipse.jetty.server.handler.DefaultHandler;import org.eclipse.jetty.server.handler.HandlerList;import org.eclipse.jetty.server.handler.ResourceHandler;import java.util.Properties;/** * Created by Nihaorz on 2017/3/26. */public class AppStart {    public static void main(String[] args) throws Exception {        long startTime = System.currentTimeMillis();        Properties prop = new Properties();        prop.load(AppStart.class.getResourceAsStream("/config/jetty.properties"));        // 设置端口        Server server = new Server(Integer.parseInt(prop.getProperty("jetty.port")));        ResourceHandler resourceHandler = new ResourceHandler();        resourceHandler.setDirectoriesListed(true);        // 设置本地磁盘路径        resourceHandler.setResourceBase(prop.getProperty("jetty.localPath"));        HandlerList handlers = new HandlerList();        handlers.setHandlers(new Handler[]{resourceHandler, new DefaultHandler()});        server.setHandler(handlers);        server.start();        long endTime = System.currentTimeMillis();        System.out.println("jetty正常启动,请访问 http://localhost:" + prop.getProperty("jetty.port"));        System.out.println("jetty服务启动耗时:" + (endTime - startTime) + "ms");        System.out.println("如需修改启动端口及本地映射路径请修改jar包中config目录下的jetty.properties");    }}

 

jetty.properties

#jetty启动端口jetty.port=9999#映射路径jetty.localPath=H:

 

然后在工程根目录执行 mvn clean compile assembly:single,就会在工程target目录下生成jar文件

使用java -jar [jar文件路径]运行即可

我打包的jar文件下载地址

jetty-server.zip

使用嵌入式jetty实现文件服务器