java web server

来源:互联网 发布:如何禁止mac地址上网 编辑:程序博客网 时间:2024/05/19 21:14

package javaweb;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Hashtable;

public class SimpleWebServer extends Thread
{
  public static final String VERSION = "KeqisoftWebServer 
http://www.keqisoft.com/";
  public static final Hashtable MIME_TYPES = new Hashtable();
  private File _rootDir;
  private ServerSocket _serverSocket;
  private boolean _running = true;

  public SimpleWebServer(File paramFile, int paramInt)
    throws IOException
  {
    this._rootDir = paramFile.getCanonicalFile();
    if (!this._rootDir.isDirectory())
      throw new IOException("Not a directory.");
    this._serverSocket = new ServerSocket(paramInt);
    start();
  }

  public void run()
  {
    while (this._running)
      try
      {
        Socket localSocket = this._serverSocket.accept();
        RequestThread localRequestThread = new RequestThread(localSocket, this._rootDir);
        localRequestThread.start();
      }
      catch (IOException localIOException)
      {
        System.exit(1);
      }
  }

  public static String getExtension(File paramFile)
  {
    String str1 = "";
    String str2 = paramFile.getName();
    int i = str2.lastIndexOf(".");
    if (i >= 0)
      str1 = str2.substring(i);
    return str1.toLowerCase();
  }

  public static void main(String[] paramArrayOfString)
  {
    try
    {
      SimpleWebServer localSimpleWebServer = new SimpleWebServer(new File("./"), 80);
    }
    catch (IOException localIOException)
    {
      System.out.println(localIOException);
    }
  }

  static
  {
    String str1 = "image/";
    MIME_TYPES.put(".gif", str1 + "gif");
    MIME_TYPES.put(".jpg", str1 + "jpeg");
    MIME_TYPES.put(".jpeg", str1 + "jpeg");
    MIME_TYPES.put(".png", str1 + "png");
    String str2 = "text/";
    MIME_TYPES.put(".html", str2 + "html");
    MIME_TYPES.put(".htm", str2 + "html");
    MIME_TYPES.put(".txt", str2 + "plain");
  }
}

/* Location:           C:\Users\Administrator\Desktop\SimpleWebServer.jar
 * Qualified Name:     org.jibble.simplewebserver.SimpleWebServer
 * JD-Core Version:    0.6.2
 */

 

package javaweb;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.URLDecoder;
import java.util.Date;
import java.util.Hashtable;

public class RequestThread extends Thread
{
  private File _rootDir;
  private Socket _socket;

  public RequestThread(Socket paramSocket, File paramFile)
  {
    this._socket = paramSocket;
    this._rootDir = paramFile;
  }

  private static void sendHeader(BufferedOutputStream paramBufferedOutputStream, int paramInt, String paramString, long paramLong1, long paramLong2)
    throws IOException
  {
    paramBufferedOutputStream.write(("HTTP/1.0 " + paramInt + " OK\r\n" + "Date: " + new Date().toString() + "\r\n" + "Server: KeqisoftWebServer/1.0\r\n" + "Content-Type: " + paramString + "\r\n" + "Expires: Thu, 01 Dec 2011 16:00:00 GMT\r\n" + (paramLong1 != -1L ? "Content-Length: " + paramLong1 + "\r\n" : "") + "Last-modified: " + new Date(paramLong2).toString() + "\r\n" + "\r\n").getBytes());
  }

  private static void sendError(BufferedOutputStream paramBufferedOutputStream, int paramInt, String paramString)
    throws IOException
  {
    paramString = paramString + "<hr>" + "KeqisoftWebServer 
http://www.keqisoft.com/";
    sendHeader(paramBufferedOutputStream, paramInt, "text/html", paramString.length(), System.currentTimeMillis());
    paramBufferedOutputStream.write(paramString.getBytes());
    paramBufferedOutputStream.flush();
    paramBufferedOutputStream.close();
  }

  public void run()
  {
    BufferedInputStream localBufferedInputStream = null;
    try
    {
      this._socket.setSoTimeout(30000);
      BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(this._socket.getInputStream()));
      BufferedOutputStream localBufferedOutputStream = new BufferedOutputStream(this._socket.getOutputStream());
      String str1 = localBufferedReader.readLine();
      if ((str1 == null) || (!str1.startsWith("GET ")) || ((!str1.endsWith(" HTTP/1.0")) && (!str1.endsWith("HTTP/1.1"))))
      {
        sendError(localBufferedOutputStream, 500, "Invalid Method.");
        return;
      }
      String str2 = str1.substring(4, str1.length() - 9);
      File localObject1 = new File(this._rootDir, URLDecoder.decode(str2, "UTF-8")).getCanonicalFile();
      File file2;
      if (((File)localObject1).isDirectory())
      {
        file2 = new File((File)localObject1, "index.html");
        if ((((File)file2).exists()) && (!((File)file2).isDirectory()))
          localObject1 = file2;
      }
      if (!((File)localObject1).toString().startsWith(this._rootDir.toString()))
      {
        sendError(localBufferedOutputStream, 403, "Permission Denied.");
      }
      else if (!((File)localObject1).exists())
      {
        sendError(localBufferedOutputStream, 404, "File Not Found.");
      }
      else
      {
        Object localObject3;
        int i;
        if (((File)localObject1).isDirectory())
        {
          if (!str2.endsWith("/"))
            str2 = str2 + "/";
          File file[] = ((File)localObject1).listFiles();
          sendHeader(localBufferedOutputStream, 200, "text/html", -1L, System.currentTimeMillis());
          localObject3 = "Keqisoft Tools";
          localBufferedOutputStream.write(("<html><head><title>" + (String)localObject3 + "</title></head><body><h3>Keqisoft Tools </h3><p>\n").getBytes());
          for (i = 0; i < file.length; i++)
          {
            localObject1 = file[i];
            String str3 = ((File)localObject1).getName();
            if("JavaWeb.jar".equals(str3)){
                continue;
            }
            String str4 = "";
            if (((File)localObject1).isDirectory())
              str4 = "&lt;DIR&gt;";
            localBufferedOutputStream.write(("<a href=\"" + str2 + str3 + "\">" + str3 + "</a> " + str4 + "<br>\n").getBytes());
          }
          localBufferedOutputStream.write("</p><hr><p>Keqisoft Web Server 
http://www.keqisoft.com/</p></body><html>".getBytes());
        }
        else
        {
          localBufferedInputStream = new BufferedInputStream(new FileInputStream((File)localObject1));
          String str = (String)SimpleWebServer.MIME_TYPES.get(SimpleWebServer.getExtension((File)localObject1));
          if (str == null)
            str = "application/octet-stream";
          sendHeader(localBufferedOutputStream, 200, (String)str, ((File)localObject1).length(), ((File)localObject1).lastModified());
          localObject3 = new byte[4096];
          while ((i = localBufferedInputStream.read((byte[])localObject3)) != -1)
            localBufferedOutputStream.write((byte[])localObject3, 0, i);
          localBufferedInputStream.close();
        }
      }
      localBufferedOutputStream.flush();
      localBufferedOutputStream.close();
    }
    catch (IOException localIOException)
    {
      if (localBufferedInputStream != null)
        try
        {
          localBufferedInputStream.close();
        }
        catch (Exception localException)
        {
        }
    }
  }
}

/* Location:           C:\Users\Administrator\Desktop\SimpleWebServer.jar
 * Qualified Name:     org.jibble.simplewebserver.RequestThread
 * JD-Core Version:    0.6.2
 */

 

http://www.jibble.org/miniwebserver/

原创粉丝点击