java http server from http://pont.net

//***************************************
// HTTP Server 
// fpont June 2000
// server implements HTTP GET method
//***************************************


import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.*;


public class httpServer 
{
    
    public static void main(String args[]) {
	
	int port;
	ServerSocket server_socket;
	
	
	try { 
	    port = Integer.parseInt(args[0]);
	}
	catch (Exception e) {
	    port = 1500;
	}
	

	try {
	    
	    server_socket = new ServerSocket(port);
	    System.out.println("httpServer running on port " + 
			       server_socket.getLocalPort());
	    
	    // server infinite loop
	    while(true) {
		Socket socket = server_socket.accept();
		System.out.println("New connection accepted " +
				   socket.getInetAddress() +
				   ":" + socket.getPort());
		
		// Construct handler to process the HTTP request message.
		try {
		    httpRequestHandler request = 
			new httpRequestHandler(socket);
		    // Create a new thread to process the request.
		    Thread thread = new Thread(request);
		    
		    // Start the thread.
		    thread.start();
		}
		catch(Exception e) {
		    System.out.println(e);
		}
	    }
	}
	
	catch (IOException e) {
	    System.out.println(e);
	}
    }
}


 
class httpRequestHandler implements Runnable
{
    final static String CRLF = "/r/n";
    Socket socket;
    InputStream input;
    OutputStream output;
    BufferedReader br;

    // Constructor
    public httpRequestHandler(Socket socket) throws Exception 
    {
	this.socket = socket;
	this.input = socket.getInputStream();
	this.output = socket.getOutputStream();
	this.br = 
	    new BufferedReader(new InputStreamReader(socket.getInputStream()));
    }
    
    // Implement the run() method of the Runnable interface.
    public void run()
    {
	try {
	    processRequest();
	}
	catch(Exception e) {
	    System.out.println(e);
	}
    }
    
    private void processRequest() throws Exception
    {
	while(true) {
	    
	    String headerLine = br.readLine();
	    System.out.println(headerLine);
	    if(headerLine.equals(CRLF) || headerLine.equals("")) break;
	    
	    StringTokenizer s = new StringTokenizer(headerLine);
	    String temp = s.nextToken();
	    
	    if(temp.equals("GET")) {

		String fileName = s.nextToken();
		fileName = "." + fileName ;
		
		// Open the requested file.
		FileInputStream fis = null ;
		boolean fileExists = true ;
		try 
		    {
			fis = new FileInputStream( fileName ) ;
		    } 
		catch ( FileNotFoundException e ) 
		    {
			fileExists = false ;
		    }
		
		// Construct the response message.
		String serverLine = "Server: fpont simple java httpServer";
		String statusLine = null;
		String contentTypeLine = null;
		String entityBody = null;
		String contentLengthLine = "error";
		if ( fileExists )
		    {
			statusLine = "HTTP/1.0 200 OK" + CRLF ;
			contentTypeLine = "Content-type: " + 
			    contentType( fileName ) + CRLF ;
			contentLengthLine = "Content-Length: " 
			    + (new Integer(fis.available())).toString() 
			    + CRLF;
		    }
		else
		    {
			statusLine = "HTTP/1.0 404 Not Found" + CRLF ;
			contentTypeLine = "text/html" ;
			entityBody = "<HTML>" + 
			    "<HEAD><TITLE>404 Not Found</TITLE></HEAD>" +
			    "<BODY>404 Not Found" 
			    +"<br>usage:http://yourHostName:port/"
			    +"fileName.html</BODY></HTML>" ;
		    }
		
		// Send the status line.
		output.write(statusLine.getBytes());
		
		// Send the server line.
		output.write(serverLine.getBytes());
		
		// Send the content type line.
		output.write(contentTypeLine.getBytes());
		
		// Send the Content-Length
		output.write(contentLengthLine.getBytes());
		
		// Send a blank line to indicate the end of the header lines.
		output.write(CRLF.getBytes());
		
		// Send the entity body.
		if (fileExists)
		    {
			sendBytes(fis, output) ;
			fis.close();
		    }
		else
		    {
			output.write(entityBody.getBytes());
		    }
				
	    }
	}
	
	try {
	    output.close();
	    br.close();
	    socket.close();
	}
	catch(Exception e) {}
    }
    
    private static void sendBytes(FileInputStream fis, OutputStream os) 
	throws Exception
    {
	// Construct a 1K buffer to hold bytes on their way to the socket.
	byte[] buffer = new byte[1024] ;
	int bytes = 0 ;
	
	// Copy requested file into the socket's output stream.
	while ((bytes = fis.read(buffer)) != -1 ) 
	    {
		os.write(buffer, 0, bytes);
	    }
    }
    
    private static String contentType(String fileName)
    {
	if (fileName.endsWith(".htm") || fileName.endsWith(".html"))
	    {
		return "text/html";
	    }
	
	return "";
	
    }
    
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值