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




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


A simple, tiny, nicely embeddable HTTP server in Java


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 “” file for the distribution license (Modified BSD licence)


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

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


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