Path: blob/master/test/jdk/com/sun/net/httpserver/SimpleFileServer.java
41152 views
/*1* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.*;24import java.util.concurrent.*;25import java.util.logging.*;26import java.io.*;27import java.net.*;28import java.security.*;29import javax.net.ssl.*;30import com.sun.net.httpserver.*;3132/**33* Implements a basic static content HTTP server34* which understands text/html, text/plain content types35*36* Must be given an abs pathname to the document root.37* Directory listings together with text + html files38* can be served.39*40* File Server created on files sub-path41*42* Echo server created on echo sub-path43*/44public class SimpleFileServer {4546public static void main (String[] args) throws Exception {47if (args.length != 3) {48System.out.println ("usage: java FileServerHandler rootDir port logfilename");49System.exit(1);50}51Logger logger = Logger.getLogger("com.sun.net.httpserver");52ConsoleHandler ch = new ConsoleHandler();53logger.setLevel(Level.ALL);54ch.setLevel(Level.ALL);55logger.addHandler(ch);5657String rootDir = args[0];58int port = Integer.parseInt (args[1]);59String logfile = args[2];60HttpServer server = HttpServer.create (new InetSocketAddress (port), 0);61HttpHandler h = new FileServerHandler (rootDir);62HttpHandler h1 = new EchoHandler ();6364HttpContext c = server.createContext ("/files", h);65c.getFilters().add (new LogFilter (new File (logfile)));66HttpContext c1 = server.createContext ("/echo", h1);67c.getFilters().add (new LogFilter (new File (logfile)));68c1.getFilters().add (new LogFilter (new File (logfile)));69server.setExecutor (Executors.newCachedThreadPool());70server.start ();71}72}737475