IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    NanoHTTPD 源码只有一个JAVA文件的轻型Web服务器

    *开心果*发表于 2013-11-30 10:42:08
    love 0

    今天小弟问我用过NanoHTTPD没有,想在Android上面使用它。百度了下,发现是Apache下面的一个开源项目。于是好奇的看了下,根据介绍说是一个小巧轻型的WEB服务器。它小到什么程度呢,小到源码只有一个JAVA文件,给人一种87V5的感觉。

    通过继承这个源码类并实现serve接口,就可以完成web服务器的基本功能。

    如果你的项目需要一个简单的静态web服务,相信它是一个不错的选择,当然可以发挥你的聪明才智使用到其它地方,比如移动设备、智能设备等。

    如果你想快速搭建起来使用一个简单的DEMO,那么它的源码里 SimpleWebServer 肯定会让你惊喜的。

    以下是唯一的一个源码文件的描述。虽然是英文,但是它还是很简单的~~

    A simple, tiny, nicely embeddable HTTP server in Java

    NanoHTTPD

    Copyright (c) 2012-2013 by Paul S. Hawke, 2001,2005-2013 by Jarno Elonen, 2010 by Konstantinos Togias

    Features + limitations:

    • Only one Java file
    • Java 5 compatible
    • Released as open source, Modified BSD licence
    • No fixed config files, logging, authorization etc. (Implement yourself if you need them.)
    • Supports parameter parsing of GET and POST methods (+ rudimentary PUT support in 1.25)
    • Supports both dynamic content and file serving
    • Supports file upload (since version 1.2, 2010)
    • Supports partial content (streaming)
    • Supports ETags
    • Never caches anything
    • Doesn’t limit bandwidth, request time or simultaneous connections
    • Default code serves files and shows all HTTP parameters and headers
    • File server supports directory listing, index.html and index.htm
    • File server supports partial content (streaming)
    • File server supports ETags
    • File server does the 301 redirection trick for directories without ‘/’
    • File server supports simple skipping for files (continue download)
    • File server serves also very long files without memory overhead
    • Contains a built-in list of most common mime types
    • All header names are converted lowercase so they don’t vary between browsers/clients

    How to use:

    • Subclass and implement serve() and embed to your own program

    See the separate “LICENSE.md” file for the distribution license (Modified BSD licence)

    >>>>>>>>

    SimpleWebServer 这个例子是有一个小小的问题的。

    采用POST方式不能获取参数值,也就是通过 session.getParms 获取不相应Form表单中的参数值。原因是因为serve 这个覆盖实现没有对POST上来的参数进行解析,当然如果POST上来的文件更没有解析,更拿不到上传的文件。通过查看父类里实现,可以找到解决方案。

    nanohttpd1

    那么子类(SimpleWebServer )只需要做如下修改即可

    nanohttpd2



沪ICP备19023445号-2号
友情链接