public final class SimpleFileServer extends Object
A simple file server is composed of three components:
HttpServer
that is bound to a given address, HttpHandler
that serves files from a given directory path, and Filter
that prints log messages relating to the exchanges handled by the server. The createFileServer
static factory method returns an HttpServer
that is a simple out-of-the-box file server. The server comes with an initial handler that serves files from a given directory path (and its subdirectories). The output level determines what log messages are printed to System.out
, if any.
Example of a simple file server:
var addr = new InetSocketAddress(8080);
var server = SimpleFileServer.createFileServer(addr, Path.of("/some/path"), OutputLevel.INFO);
server.start();
The createFileHandler
static factory method returns an HttpHandler
that serves files and directory listings. The handler supports only the HEAD and GET request methods; to handle other request methods, one can either add additional handlers to the server, or complement the file handler by composing a single handler via HttpHandlers.handleOrElse(Predicate, HttpHandler, HttpHandler)
.
Example of composing a single handler:
var handler = HttpHandlers.handleOrElse(
(req) -> req.getRequestMethod().equals("PUT"),
(exchange) -> {
// validate and handle PUT request
},
SimpleFileServer.createFileHandler(Path.of("/some/path")))
);
The createOutputFilter
static factory method returns a post-processing filter
that prints log messages relating to the exchanges handled by the server. The output format is specified by the outputLevel
.
Example of an output filter:
var filter = SimpleFileServer.createOutputFilter(System.out, OutputLevel.VERBOSE);
var server = HttpServer.create(new InetSocketAddress(8080), 10, "/some/path/", new SomeHandler(), filter);
server.start();
A simple HTTP file server implementation is provided via the jwebserver
tool.
Modifier and Type | Class | Description |
---|---|---|
static enum |
SimpleFileServer.OutputLevel |
Describes the log message output level produced by the server when processing exchanges. |
Modifier and Type | Method | Description |
---|---|---|
static HttpHandler |
createFileHandler |
Creates a file handler that serves files from a given directory path (and its subdirectories). |
static HttpServer |
createFileServer |
Creates a file server that serves files from a given path. |
static Filter |
createOutputFilter |
Creates a post-processing Filter that prints log messages about exchanges. |
public static HttpServer createFileServer(InetSocketAddress addr, Path rootDirectory, SimpleFileServer.OutputLevel outputLevel)
The server is configured with an initial context that maps the URI path
to a file handler. The file handler is created as if by an invocation of createFileHandler(rootDirectory)
, and is associated to a context created as if by an invocation of createContext("/")
. The returned server is not started.
An output level can be given to print log messages relating to the exchanges handled by the server. The log messages, if any, are printed to System.out
. If OutputLevel.NONE
is given, no log messages are printed.
addr
- the address to listen onrootDirectory
- the root directory to be served, must be an absolute pathoutputLevel
- the log message output levelIllegalArgumentException
- if root does not exist, is not absolute, is not a directory, or is not readableUncheckedIOException
- if an I/O error occursNullPointerException
- if any argument is nullSecurityException
- if a security manager is installed and a recursive FilePermission
"read
" of the rootDirectory is deniedpublic static HttpHandler createFileHandler(Path rootDirectory)
The file handler resolves the request URI against the given rootDirectory
path to determine the path p
on the associated file system to serve the response. If the path p
is a directory, then the response contains a directory listing, formatted in HTML, as the response body. If the path p
is a file, then the response contains a "Content-Type" header based on the best-guess content type, as determined by an invocation of getContentTypeFor, on the system-wide mimeTable
, as well as the contents of the file as the response body.
The handler supports only requests with the HEAD or GET method, and will reply with a 405
response code for requests with any other method.
rootDirectory
- the root directory to be served, must be an absolute pathIllegalArgumentException
- if rootDirectory does not exist, is not absolute, is not a directory, or is not readableNullPointerException
- if the argument is nullSecurityException
- if a security manager is installed and a recursive FilePermission
"read
" of the rootDirectory is deniedpublic static Filter createOutputFilter(OutputStream out, SimpleFileServer.OutputLevel outputLevel)
OutputStream
in UTF-8
encoding.out
- the stream to print tooutputLevel
- the output levelIllegalArgumentException
- if OutputLevel.NONE
is givenNullPointerException
- if any argument is null
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/jdk.httpserver/com/sun/net/httpserver/SimpleFileServer.html