Question

I have made a working web server with jetty on an android device, but it only displays a static website, does any one know how to display a folder content of the android device? can it be done with a web page or there is a simpler way to do it? I want it similar to when you connect to a tcp server

Was it helpful?

Solution

Serving static content with Jetty can be done using a ResourceHandler.

A simple example

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ResourceHandler;


public class FolderContents {
  public static void main(String[] args) throws Exception {
    Server server = new Server(8080);

    ContextHandler requestContext = new ContextHandler("/filelist");

    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setResourceBase("/path/to/directory/");
    resourceHandler.setDirectoriesListed(true);

    requestContext.setHandler(resourceHandler);

    server.setHandler(requestContext);

    server.start();
    server.join();
  }
}

So, on a request to /filelist/, the directory contents will be printed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top