首页 > 编程语言 >使用Java写一个简易web服务器

使用Java写一个简易web服务器

时间:2023-11-17 18:11:06浏览次数:27  
标签:web Java ServerConfig server ServerConfigLoader 服务器 serverConfig public

使用Java写一个简易web服务器来替代nginx功能。

main:

    public static void main(String[] args) {
        ServerConfigLoader serverConfigLoader = Factory.serverConfigLoader();
        ServerConfig serverConfig = serverConfigLoader.getServerConfig();
        HttpServer httpServer = SimpleFileServer.createFileServer(new InetSocketAddress(Integer.parseInt(serverConfig.serverPort())),
                Path.of(serverConfig.contextLocation()), SimpleFileServer.OutputLevel.VERBOSE);
        httpServer.start();
    }

config:

public class ServerConfigLoader {

    private ServerConfig serverConfig;

    public ServerConfigLoader() {
        String configFileName = "server.properties";
        try (InputStream input = ServerConfigLoader.class.getClassLoader().getResourceAsStream(configFileName)) {
            Properties prop = new Properties();
            prop.load(input);
            this.serverConfig = new ServerConfig(prop.getProperty("server.port"), prop.getProperty("context.location"));
        } catch (IOException ex) {
            System.out.println("Start web server failed: " + ex.getLocalizedMessage());
            ex.printStackTrace();
        }
    }

    public ServerConfig getServerConfig() {
        return serverConfig;
    }
}


public record ServerConfig(String serverPort, String contextLocation) {
}

public class Factory {

    public static ServerConfigLoader serverConfigLoader() {
        return new ServerConfigLoader();
    }

}

到此,极简web server就写完了。
配置文件:
server.properties

server.port=8080
context.location=D:\\your-dictionary

标签:web,Java,ServerConfig,server,ServerConfigLoader,服务器,serverConfig,public
From: https://www.cnblogs.com/jiayuan2006/p/17839433.html

相关文章

  • 服务器使用 compressAi
    这个命令pipinstallcompressai似乎不行要在服务器中使用命令gitclonehttps://github.com/InterDigitalInc/CompressAIcompressaicdcompressaipipinstall-Upip&&pipinstall-e.......
  • javascript 自定义分页组件
    仿boostrap前端分页组件的实现一 写一个前端自定义分页组件,需要考虑以下问题  /*     需要一个<ul id="pagination"></ul>标签   total; // 总数据的数量   pageSize; // 一页显示数量   pageIndex; // 当前页   */ 二实现细节编写html......
  • beego:将服务器上的图片显示到客户端浏览器
    来源:http://www.shanhubei.com/archives/2840.html将服务器上的图片显示到客户端浏览器//@Title//@Description显示图片//@Paramnumquerystringtrue比如:图片名字20181120164548_75_jd.jpg//@Success200//@router/ShowImage[get]func(u......
  • (javascript)将ztree树结构的数据转成二维数组
    ztree树结构的数据结构如下:[{"id":3990,"name":"泡沫灭火","iconShow":false,"children":[{"id":8616,......
  • 利用 Webpack CodeSplitting 完成复杂应用拆包
    AllinOne的弊端通过Webpack实现前端项目整体模块化的优势固然明显,但是它也会存在一些弊端:它最终会将所有的代码打包到一起。试想一下,如果应用非常复杂,模块非常多,那么这种AllinOne的方式就会导致打包的结果过大,甚至超过4~5M。在绝大多数的情况下,应用刚开始工作时,并不......
  • java zip 压缩密码
    在Java开发中,Zip文件是一种常见的文件压缩格式。有时候我们需要给Zip文件添加密码保护,以确保文件的安全性。本文将指导你如何使用Java代码来实现Zip文件的添加密码功能。流程概述下面是实现“JavaZip添加密码”的整个流程:步骤描述步骤1创建一个Zip文件步骤2向Zi......
  • C# RSA2 SHA256 对应JAVA(SHA256withRSA)公钥验签名,CER公钥证书
    Stringtimestamp="1630905585";Stringnonce="9003323344";Stringsignature="tnjIAcEISq/ClrOppv/nojeZnE/pB1wNfQC/hMTME+rQMapWzvs9v1J68ueDpVzs1RW22dNotmUVy2sM6thNFRkaOx4qQGslX6kIttwvlsJsSEIR3qrjdPdUAkbP2KDRLujspxE9X0daJ6BU+......
  • Core 6.0 webapi ‘报错InvalidOperationException:无法解析“ Microsoft.AspNetCore.H
    因接口版本升级并使用core6.0却发现HttpContext.Current.Request用不了 所以在网上找了半天说是使用Microsoft.AspNetCore.Http.IHttpContextAccessorprivateIHttpContextAccessor_httpContextAccessor;publicWebHelper(IHttpContextAccessorhttpContextAccessor......
  • 二维码保存到本地Java代码
    工具类importjava.io.ByteArrayInputStream;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;publicclassWxaQrCodeUtil{/***二维码保存到本地*@parambytes*@parampath保......
  • JavaScript之splice()
    参考:https://blog.csdn.net/weixin_45726044/article/details/120151153概述splice()方法通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。此方法会改变原数组。一个参数splice(index)——>从index的位置开始,删除之后的所有元素(包括......