📄 httpfileserver.java
字号:
package it.unimi.dsi.mg4j.query;/* * MG4J: Managing Gigabytes for Java * * Copyright (C) 2005-2007 Sebastiano Vigna * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */import javax.activation.MimetypesFileTypeMap;import org.mortbay.http.HttpContext;import org.mortbay.http.HttpServer;import org.mortbay.http.SocketListener;import org.mortbay.http.handler.ResourceHandler;/** A minimal, singleton server serving the whole filesystem. * * <p>For security reasons, some browsers (notably Firefox at the time of this writing) * do not serve <samp>file:</samp> content from a non-<samp>file:</samp> page. The solution * is a minimal server that has the root of the filesystem as document root. * * <p>To instantiate the server, you just need a variable of type {@link HttpFileServer}. * just retrieve and cache the result of {@link #getServer()} (to * avoid class garbage collection). The port randomly assigned to the server can be retrieved with * {@link #getPort()}. * * <p>It may happen that the server is not yet started when you call {@link #getPort()}, in which case * an {@link java.lang.IllegalStateException} will be thrown. */public class HttpFileServer { /** The only server instance. */ private final static HttpServer SERVER; /** The only instance of this class. */ private final static HttpFileServer INSTANCE = new HttpFileServer(); /** The dynamically assigned port, or 0 if the port has not been detected yet. */ private static int port; /** An instance of {@link MimetypesFileTypeMap} used to recognise MIME types. */ @SuppressWarnings("unused") private final static MimetypesFileTypeMap MIME_TYPES_FILE_TYPE_MAP = new MimetypesFileTypeMap(); static { // This allows symlinks. System.setProperty( "org.mortbay.util.FileResource.checkAliases", Boolean.FALSE.toString() ); } /** Returns the only instance of an HTTP file server. * * <p>Note that the instance should be cached by the application, to avoid garbage collection. * * @return the only instance of an HTTP file server */ public static HttpFileServer getServer() { return INSTANCE; } /** Returns the port assigned to the server. * * @return the port assigned to the server. */ public static int getPort() { if ( port == 0 ) { if ( ! SERVER.isStarted() ) throw new IllegalStateException( "The server is not started yet" ); port = SERVER.getListeners()[ 0 ].getPort(); } return port; } private HttpFileServer() {} static { // Create the server SERVER = new HttpServer(); // Create a port listener final SocketListener listener = new SocketListener(); listener.setPort( 0 ); SERVER.addListener( listener ); // Create a context final HttpContext context = new HttpContext(); context.setContextPath( "/" ); context.setResourceBase( "/" ); SERVER.addContext( context ); // Add a resource handler ResourceHandler resourceHandler = new ResourceHandler(); resourceHandler.setAllowedMethods( new String[] { "GET" } ); context.addHandler( resourceHandler ); // Start the http server new Thread() { public void run() { try { SERVER.start(); } catch ( Exception e ) { throw new RuntimeException(); } } }.start(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -