WebServer in Java

package com.Web;

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

public class WebServer extends Thread {
    String filePath;
    OutputStream out;
    Socket sock;
    public WebServer(String pathString, OutputStream outStream, Socket s) {
        filePath = pathString;
        sock = s;
        out = outStream;
    }
    public void run() {
        PrintWriter writer = new PrintWriter(out, true);
        try {
            filePath = (new File(filePath)).getAbsolutePath();
            InputStream in = new FileInputStream(filePath);
            int val;
            writer.println(“HTTP/1.0 200 OK”);
            writer.println(“”);
            System.out.println(“<Client> Uploading : ” + filePath);
            while ((val = in.read()) != -1) {
                out.write(val);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            writer.println(“<html><body><b>” + e.getMessage()
                    + “</b></body></html>”);
            System.out.println(“<Client> ” + e.getMessage());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            writer.println(“<html><body><b>” + e.getMessage()
                    + “</b></body></html>”);
            System.out.println(“<Client> ” + e.getMessage());
        }
        writer.println(“”);
        System.out.println(“<Client> Uploading : Complete”);
        System.out.println(“<Client> “);
        try {
            sock.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println(“ERROR: ” + e.getMessage());
        }
    }

    public static void main(String[] args) throws Exception {
        ServerSocket server = new ServerSocket(80);
        while (true) {
            Socket s = server.accept();
            OutputStream out = s.getOutputStream();
            InputStream in = s.getInputStream();
            InputStreamReader inReader = new InputStreamReader(in);
            BufferedReader reader = new BufferedReader(inReader);
            String line = reader.readLine();
            while (line != null && !””.equals(line)) {
                System.out.println(“<Client> ” + line);
                String splitString[] = line.split(” “);
                if (splitString[0].equals(“GET”)) {
                    WebServer webServer = new WebServer(“.” + splitString[1],
                            out, s);
                    webServer.start();
                }
                line = reader.readLine();
            }
            System.out.println(“<Client> “);
        }
    }
}

It is your duty to compile and make it working condition… use JDK1.6 and eclipse…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s