1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.ajlopez;

import java.io.*;
import java.net.*;

public class HttpServer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int port;
		ServerSocket serversocket;
		
		port = Integer.parseInt(args[0]);
		String rootpath = args[1];
		
		try {
			serversocket = new ServerSocket(port);
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
		
		while (true) {
			try {
				Socket socket = serversocket.accept();
				BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				String line = reader.readLine();
				String [] words = line.split(" ");
				System.out.println(line);
				InputStream stream = new FileInputStream(rootpath + words[1]);
				OutputStream output = socket.getOutputStream();
				byte [] buffer = new byte[4096];
				int nbytes;
				
				while ((nbytes = stream.read(buffer))!=-1)
					output.write(buffer,0,nbytes);
				
				output.close();
				stream.close();
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}