ConnectorTest.java
~~~~~~~~~~~~~~~
package com.anils.test;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Simulator HTTP Server, can accept user request, according request send
* response, ConnectorTest where main ServerSocket
*
*
*/
public class ConnectorTest implements Runnable {
private final int port;
private ServerSocket serverSocket;
public ConnectorTest(int port) {
this.port = port;
try {
/*
* create serverSocket, listen on special port
*/
serverSocket = new ServerSocket(port);
new Thread(this).start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
try {
/*
* wait client connection
*/
Socket socket = serverSocket.accept();
new HttpdTest(socket);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* main method, entry of system
*/
public static void main(String[] args) {
new ConnectorTest(8888);
}
}
HttpdTest.java
~~~~~~~~~~~~~~
package com.anils.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
/**
* Simulator HTTP Server, can accept user request, according request send
* response, HttpdTest were access thread
*
*/
public class HttpdTest implements Runnable {
private final Socket socket;
public static final String BASE_PATH = "d:/root";
/**
* construct method
*
* @param socket
* , client connect socket
*/
public HttpdTest(Socket socket) {
this.socket = socket;
new Thread(this).start();
}
/*
* main access method, access request, send response, then close
*/
public void run() {
// access request
// access response
// close
try {
/*
* get input stream and output stream
*/
InputStream inputStream = socket.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
OutputStream outputStream = socket.getOutputStream();
PrintWriter writer = new PrintWriter(outputStream);
/*
* get first line, http head message
*/
String headLine = reader.readLine();
/*
* split head message, get method, request file and HTTP protocol
*/
String[] split = headLine.split(" ");
/*
* if is not GET method, can't access
*/
if (!"GET".equals(split[0])) {
// can't access
}
String path = split[1];
/*
* if end folder where '/', then add default index.html to path
*/
if (path.endsWith("/")) {
path = path + "index.html";
}
/*
* get absolute path of file
*/
String filename = BASE_PATH + path;
File file = new File(filename);
/*
* check request file exist, if not exist, response not found page
* message
*/
if (!file.exists() || file.isDirectory()) {
/*
* send http response head format
*/
writer.println("HTTP/1.1 404 ok");
writer.println();
writer.flush();
socket.close();
return;
}
/*
* send request file found message
*/
writer.println("HTTP/1.1 200 ok");
String lowerName = filename.toLowerCase();
/*
* according file extention, send http head message
*/
if (lowerName.endsWith(".txt") || lowerName.endsWith(".html")) {
writer.println("Content-Type: text/html");
} else if (lowerName.endsWith(".jpg")) {
writer.println("Content-Type: image/jpeg");
}
writer.println();
if (lowerName.endsWith(".txt") || lowerName.endsWith(".html")) {
/*
* if file ends with .txt or .html, get request file, read file
* content, and send content to client
*/
FileReader fileReader = new FileReader(file);
char[] buf = new char[256];
int count = 0;
while ((count = fileReader.read(buf)) != -1) {
writer.write(buf, 0, count);
}
fileReader.close();
} else if (lowerName.endsWith(".jpg")) {
/*
* if file ends with .jpg, get request jpg file, read file
* content, and send content to client
*/
FileInputStream fileInputStream = new FileInputStream(file);
byte[] buf = new byte[250];
int count = 0;
while ((count = fileInputStream.read(buf)) != -1) {
outputStream.write(buf, 0, count);
}
outputStream.flush();
}
/*
* remember call flush method
*/
writer.flush();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment