用原生Java写一个静态资源服务器

背景

公司内网的服务器层层限制,nginx依赖复杂实在装不上
无奈自己用java做了个nginx,提供负载均衡和静态资源服务

使用java做tcp转发不难,网络上有素材
需要自己补充负载均衡的策略

虽使用springboot也能比较方便的做出静态资源服务器
但是想到负载均衡都做了,干脆整合在一起

于是就有了这个项目,急用可以直接拿
https://github.com/PiaoZhenJia/Jginx

这个小项目的特点是易于安装,易于配置
缺点嘛,肯定是安全和性能啦

这是他的配置文件,上面配置了静态资源服务,下面配置了负载均衡服务
配置文件是个json,并且在首次运行会自动生成配置示例:

{
	"un_name_resource_server":{
		"listenPort":81,
		"mode":"resource",
		"path":"D:/",
		"uri":"/sample/"
	},
	"un_name_proxy_server":{
		"listenPort":80,
		"mode":"proxy",
		"remoteLocation":["localhost:8080","localhost:8081","localhost:8082"]
	}
}

欢迎使用呀

正文开始

JDK自带了一个简易的HTTP服务器,在包 com.sun.net.httpserver

我们现在要用它编写一个静态资源服务器,简单来说,就是拦截一个get请求,并返回对应的文件流。

那么这个 HttpServer 大概是如何工作的呢,我已经总结好了

  • 创建HttpServerProvider对象
  • 通过HttpServerProvider对象,创建HttpServer对象
  • 设置监听的端口
  • 设置要拦截的URL和对应的处理类 HttpHandler
  • 启动HttpServer

这里的 HttpHandler 是一个接口,我看JDK没有给默认实现,需要我们自己去实现。

先上主类的代码

import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.spi.HttpServerProvider;

import java.io.IOException;
import java.net.InetSocketAddress;

public class Main {

    public static void main(String[] args) throws IOException {
        HttpServerProvider provider = HttpServerProvider.provider();
        HttpServer server = provider.createHttpServer(new InetSocketAddress(80), 1024);//监听80端口
        server.createContext("/res/", new HttpGetHandler());// URL 与 Handler 绑定
        server.start();
        System.out.println("启动好啦");//主类在这里就执行完了,但是因为有监听线程一直运行,所以程序不会退出。
    }

}

其中 createHttpServer 方法有两个参数,第一个参数是监听80端口,第二个参数源码是这样说的

    /**
     * creates a HttpServer from this provider
     *
     * @param  addr
     *         the address to bind to. May be {@code null}
     *
     * @param  backlog
     *         the socket backlog. A value of {@code zero} means the systems default
     * @throws IOException if an I/O error occurs
     * @return An instance of HttpServer
     */
    public abstract HttpServer createHttpServer(InetSocketAddress addr,
                                                int backlog)
        throws IOException;

我理解是socket的待处理队列长度,填0是系统默认值,这里我们随便填个1024好了

Handler类代码

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import java.io.*;
import java.net.URLEncoder;

public class HttpGetHandler implements HttpHandler {
    @Override
    public void handle(HttpExchange he) throws IOException {
        OutputStream responseBody = he.getResponseBody();
        String requestMethod = he.getRequestMethod();
        Headers responseHeaders = he.getResponseHeaders();

        if (requestMethod.equalsIgnoreCase("GET")) {
            //url路径替换成文件路径
            File file = new File("D:/" + he.getRequestURI().getPath().replace("/res/", ""));
            System.out.println("开始下载");
            //判断文件是否存在
            if (!(file.exists() && file.isFile())) {
                responseHeaders.set("Content-Type", "text/html;charset=UTF-8");
                responseHeaders.set("Content-Length", "20");
                try {
                    he.sendResponseHeaders(404, 0);
                    responseBody.write("\r\n404 File Not Found ~".getBytes());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                responseHeaders.set("Content-Type", "applicatoin/octet-stream");
                try {
                    responseHeaders.set("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                responseHeaders.set("Content-Length", String.valueOf(file.length()));
                try (InputStream bis = new BufferedInputStream(new FileInputStream(file));
                     OutputStream ops = new BufferedOutputStream(responseBody)) {
                    he.sendResponseHeaders(200, 0);
                    byte[] body = new byte[1024 * 4];//缓存为4kb
                    int i;
                    while ((i = bis.read(body)) != -1) {
                        ops.write(body, 0, i);
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    System.out.println("IO连接中断");
                }
            }
        }
        try {
            responseBody.close();
            System.out.println("下载完成");
        } catch (IOException e) {
            System.out.println("异常停止");
        }
    }
}

在Handler类中,我们做了静态资源服务器的基本工作,

  • 解析URL,将找到磁盘对应路径下的文件,并通过HTTP响应给浏览器下载
  • 在判断文件不存在的情况下,会响应404的 http code,和相应简易页面
  • 设置了4k的流缓存,防止文件过大时直接读取到内存导致OOM
  • 下载时会告知浏览器文件大小(响应头中Content-Length字段)否则浏览器会显示未知大小,影响体验

这是一个最简单的静态资源服务器,他存在以下注意事项

  1. 安全性不能得到保证,即没有限制路径层级,也不能避免路径穿越
  2. HttpHandler是同步阻塞执行的,也就是说同时只能处理一个下载
  3. 一旦HttpHandler的执行抛出了异常,HttpServer便不能再处理其他任何事件

解决方式倒也简单,

  1. 可以通过正则等方式校验URL
  2. 在HttpHandler中启动一个线程去执行响应,然后直接返回
  3. 为HttpHandler添加一个大的 try-catch 块(这个工作其实已经做好了)

简单总结一下服务器为浏览器返回文件流时做的几件事

  • 找到文件
  • 声明响应头 Content-Type 为 applicatoin/octet-stream
  • 在响应头 Content-Disposition 中声明 filename 作为文件名
  • 在响应头 Content-Length 中声明文件二进制的长度
  • 在响应体中写入二进制流

多线程版本的项目我放在了文初的链接里
此文中仅实现了最简功能,用以帮助同学入手制作Http下载功能。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单的原生聊天对话界面的Java实现,你可以参考一下: ```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ChatUI extends JFrame implements ActionListener { private JTextArea chatArea; private JTextField inputField; private JButton sendButton; public ChatUI() { super("Chat"); setSize(400, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); chatArea = new JTextArea(); chatArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(chatArea); inputField = new JTextField(); inputField.addActionListener(this); sendButton = new JButton("Send"); sendButton.addActionListener(this); JPanel inputPanel = new JPanel(new BorderLayout()); inputPanel.add(inputField, BorderLayout.CENTER); inputPanel.add(sendButton, BorderLayout.EAST); getContentPane().add(scrollPane, BorderLayout.CENTER); getContentPane().add(inputPanel, BorderLayout.SOUTH); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == inputField || e.getSource() == sendButton) { String inputText = inputField.getText(); chatArea.append("You: " + inputText + "\n"); inputField.setText(""); } } public static void main(String[] args) { new ChatUI(); } } ``` 这个界面很简单,只有一个聊天区域、一个文本输入框和一个发送按钮。用户在文本框中输入消息,按下回车键或点击发送按钮后,消息就会显示在聊天区域中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

控场的朴哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值