import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
public class Processor extends Thread{
private Socket socket;
private InputStream in;
private PrintStream out;
public final static String WEB_ROOT="c:\\workspace\\webserver\\htdocs";
public Processor(Socket socket){
this.socket=socket;
try {
in=socket.getInputStream();
out=new PrintStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){
String filename=parse(in);
sendFile(filename);
}
public String parse(InputStream in){
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String filename=null;
try {
String httpMessage=br.readLine();
String[] content=httpMessage.split(" ");
if(content.length!=3){
sendErrorMessage(400,"Client query error!");
return null;
}
System.out.println("code:"+content[0]+",fileName"+content[1]+",http version:"+content[2]);
filename=content[1];
} catch (IOException e) {
e.printStackTrace();
}
return filename;
}
public void sendErrorMessage(int errorCode,String errorMessage){
out.println("HTTP/1.0"+errorCode+" "+errorMessage);
out.println("content-type: text/html");
out.println();
out.println("");
out.println("Error Message");
out.println("");
out.println("");
out.println("ErrorCode:"+errorCode+",Message:"+errorMessage+"");
out.println("");
out.println("");
out.flush();
out.close();
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendFile(String filename){
File file=new File(Processor.WEB_ROOT+filename);
if(!file.exists()){
sendErrorMessage(404,"File Not Found!");
return;
}
try {
InputStream in=new FileInputStream(file);
byte content[]=new byte[(int)file.length()];
in.read(content);
out.println("HTTP/1.0 200 queryfile");
out.println("conrtent=length:"+content.length);
out.println();
out.write(content);
out.flush();
out.close();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.WebServer.java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public void serverStart(int port) {
try {
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
Socket socket = serverSocket.accept();
new Processor(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
int port = 80;
if (args.length == 1) {
port = Integer.parseInt(args[0]);
}
new WebServer().serverStart(port);
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
public class Processor extends Thread{
private Socket socket;
private InputStream in;
private PrintStream out;
public final static String WEB_ROOT="c:\\workspace\\webserver\\htdocs";
public Processor(Socket socket){
this.socket=socket;
try {
in=socket.getInputStream();
out=new PrintStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){
String filename=parse(in);
sendFile(filename);
}
public String parse(InputStream in){
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String filename=null;
try {
String httpMessage=br.readLine();
String[] content=httpMessage.split(" ");
if(content.length!=3){
sendErrorMessage(400,"Client query error!");
return null;
}
System.out.println("code:"+content[0]+",fileName"+content[1]+",http version:"+content[2]);
filename=content[1];
} catch (IOException e) {
e.printStackTrace();
}
return filename;
}
public void sendErrorMessage(int errorCode,String errorMessage){
out.println("HTTP/1.0"+errorCode+" "+errorMessage);
out.println("content-type: text/html");
out.println();
out.println("");
out.println("Error Message");
out.println("");
out.println("");
out.println("ErrorCode:"+errorCode+",Message:"+errorMessage+"");
out.println("");
out.println("");
out.flush();
out.close();
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendFile(String filename){
File file=new File(Processor.WEB_ROOT+filename);
if(!file.exists()){
sendErrorMessage(404,"File Not Found!");
return;
}
try {
InputStream in=new FileInputStream(file);
byte content[]=new byte[(int)file.length()];
in.read(content);
out.println("HTTP/1.0 200 queryfile");
out.println("conrtent=length:"+content.length);
out.println();
out.write(content);
out.flush();
out.close();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.WebServer.java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public void serverStart(int port) {
try {
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
Socket socket = serverSocket.accept();
new Processor(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
int port = 80;
if (args.length == 1) {
port = Integer.parseInt(args[0]);
}
new WebServer().serverStart(port);
}
}