部分源码:(全部源码去上面下载)
//Server.java
package cn.net.sight.server;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;
import cn.net.sight.thread.ServerThread;
public class Server {
private static ServerSocket server;
private static Properties properties;
private int port;
static {
properties = new Properties();
try {
properties.load(new FileInputStream(new File(“src/resources/property.proterties”)));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//initialize the server
public void init() {
try {
port = Integer.parseInt(properties.getProperty(“port”));
server = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}
//receive the request from the web browser
public void receive() {
Socket clientSocket = new Socket();
try {
clientSocket = server.accept();
} catch (IOException e) {
e.printStackTrace();
}
ServerThread thread = new ServerThread(clientSocket);
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//startup server
public static void main(String[] args) {
Server Aaroncat = new Server();
Aaroncat.init();
System.out.println(“----Aaroncat has startup----”);
while(true){
Aaroncat.receive();
}
}
}
//Request.java
package cn.net.sight.server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import cn.net.sight.util.MessageUtil;
public class Request {
private InputStream input; // Socket --》 InputStream
private Socket socket; // client --》 socket
private BufferedReader buffer; // InputStream --》 BufferedReader
private String schema; // the schema of the request GET or POST
private String requestFileName; // exact file name
private String requestData; // file name + 《key=value》。。。
private String values_Str; // a string the user input in the form
private int paramLength; // using in the POST: the length of parameters
private Map《String, String》 socketValues;// values_str --》 MAP
private PrintStream print;
protected MessageUtil messageUtil = new MessageUtil();
//省略了全部的setter() getter()
private void doSchema(String firstLineInData) throws IOException {
socketValues = new HashMap《String, String》();
if (this.schema.equals(“GET”)) {
// GET请求 --》 包含文件名和参数键值对
// 实现了对FileName、SocketValues的赋值
this.setRequestData(messageUtil.getRequestData(firstLineInData));
if (this.requestData.contains(“?”)) {
this.setRequestFileName(messageUtil.getFileName(this.getRequestData()));
this.setSocketValues(messageUtil.getValues(this.getRequestData()));
} else {
// GET请求 --》只包含文件名
// 实现了对FileName的赋值
this.setRequestFileName(requestData);
}
} else {
// POST请求 第一行只包含文件名
// 实现了对FileName、SocketValues的赋值
this.setRequestFileName(messageUtil.getRequestData(firstLineInData));
this.getUserInfo(buffer);
}
}
private void getUserInfo(BufferedReader br) throws IOException {
while (this.buffer.ready()) {
String remained = buffer.readLine();
if (remained.contains(“Content-Length”)) {
String[] temp = remained.split(“ ”);
this.setParamLength(Integer.parseInt(temp[1]));
break;
}
}
buffer.readLine();
String userInfo = “”;
for (int i = 0; i 《 this.getParamLength(); i++) {
userInfo += (char) buffer.read();
}
this.setValues_Str(userInfo);
this.setSocketValues(messageUtil.getValues(this.getValues_Str()));
}
public Request(Socket clientSocket) throws IOException {
this.setSocket(clientSocket);
this.setPrint(new PrintStream(clientSocket.getOutputStream()));
this.setInput(clientSocket.getInputStream());
this.setBuffer(new BufferedReader(new InputStreamReader(clientSocket.getInputStream())));
// get something from the first line
String firstLineInData = buffer.readLine();
this.setSchema(messageUtil.getSchema(firstLineInData)); // 获得请求方式Schema
doSchema(firstLineInData); // 对Schema进行判断
}
}
//Response.java
package cn.net.sight.server;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Date;
import java.util.Map;
import cn.net.sight.util.FileUtil;
import cn.net.sight.util.LoginUtil;
public class Response {
private String fileName;
private Map《String, String》 userValues;
private PrintStream ps;
private Request request;
private Socket clientSocket;
protected FileUtil fileUtil = new FileUtil();
protected LoginUtil loginUtil = new LoginUtil();
public String getFileName() {
return fileName;
}
//省略了全部的setter() 和 getter()
public Response(Request request) {
this.setRequest(request);
this.setClientSocket(request.getSocket());
this.setFileName(request.getRequestFileName());
userValues = this.request.getSocketValues();
try {
this.init();
} catch (IOException e) {
e.printStackTrace();
}
}
public void showup(String fileName) throws IOException {
this.ps = this.request.getPrint();
// 要处理正常文件名和空文件名
if (!fileName.equals(“”) && !fileName.equals(“error.html”)) {
this.ps.println(“HTTP/1.1 200 OK”);
this.ps.println();
fileUtil.readFile(fileName, ps);
} else if (fileName.equals(“error.html”)) {
ps.println(“HTTP/1.1 404 fileNotFound”);
ps.println();
fileUtil.readFile(“error.html”, ps);
} else {
ps.println(new Date().toString());
}
if (ps != null){
ps.close();
}
}
public void init() throws IOException {
//如果信息MAP是空,则代表是普通文件或者是默认访问
if (userValues.isEmpty()) {
if (fileName != “”) {
this.showup(fileName);
} else {
this.ps = this.request.getPrint();
ps.println(new Date().toString());
if(ps != null) ps.close();
if(clientSocket != null)clientSocket.close();
}
} else {
//如果信息MAP不为空,代表是GET/POST请求,并带有参数键值对
this.Check(userValues, fileName);
}
}
public void Check(Map《String, String》 values_list, String respFileName) throws IOException {
// 验证用户输入信息的合法性
if (loginUtil.isValid(values_list)) {
this.showup(respFileName);
} else {
this.showup(“error.html”);
}
}
}
//ServerThread.java
package cn.net.sight.thread;
import java.io.IOException;
import java.net.Socket;
import cn.net.sight.server.Response;
import cn.net.sight.server.Request;
public class ServerThread extends Thread {
private Socket clientSocket;
private Request request;
private Response response;
public ServerThread() {
super();
}
public ServerThread(Socket clientSocket) {
super();
this.clientSocket = clientSocket;
}
public Socket getClientSocket() {
return clientSocket;
}
public void setClientSocket(Socket clientSocket) {
this.clientSocket = clientSocket;
}
public Request getRequest() {
return request;
}
public void setRequest(Request request) {
this.request = request;
}
public Response getResponse() {
return response;
}
public void setResponse(Response response) {
this.response = response;
}
public void run(){
super.run();
try {
this.setRequest(new Request(clientSocket));
this.response = new Response(request);
this.setResponse(response);
} catch (IOException e) {
e.printStackTrace();
}
}
}
//FileUtil.java
package cn.net.sight.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
public class FileUtil {
private static final int BUFFER_SIZE = 1024;
public void readFile(String file_Name, PrintStream ps) throws IOException {
byte[] buffer = new byte[BUFFER_SIZE];
int length;
File file = new File(“src/resources/” + file_Name);
FileInputStream fis = null;
if (file.exists()) {
try {
fis = new FileInputStream(file);
while ((length = fis.read(buffer)) != -1) {
ps.write(buffer, 0, length);
ps.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else {
ps.println(“File not found”);
ps.println();
}
}
}
//LoginUtil.java
package cn.net.sight.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
public class LoginUtil {
protected boolean flag = false;
protected Map《String, String》 values;
private static Properties properties;
static {
properties = new Properties();
try {
properties.load(new FileInputStream(new File(“src/resources/property.proterties”)));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public Map《String, String》 getValues() {
return values;
}
public void setValues(Map《String, String》 values) {
this.values = values;
}
// 验证用户信息的合法性(应用JDBC桥,连接数据库)
public boolean isValid(Map《String, String》 values) {
String username = properties.getProperty(“username”);
String password = properties.getProperty(“password”);
if (values.get(“username”).equals(username)) {
if (values.get(“password”).equals(password)) {
flag = true;
System.out.println(“The user ” + values.get(“username”) + “ was log the server.”);
return flag;
}
} else {
System.out.println(“Forbide the ” + values.get(“username”) + “ log the server”);
return flag;
}
return false;
}
}
//MessageUtil.java
package cn.net.sight.util;
import java.util.HashMap;
import java.util.Map;
public class MessageUtil {
// schema : GET or POST
public String getSchema(String requestMsg) {
String[] result = new String[1];
if (requestMsg.contains(“ ”)) {
result = requestMsg.split(“ ”);
requestMsg = result[0];
}
return requestMsg;
}
// get the resquestData = (filename + map《S,S》)
public String getRequestData(String firstLineInData) {
String[] result = new String[10];
result = firstLineInData.split(“ ”);
firstLineInData = result[1].substring(1);
return firstLineInData;
}
// get the filename from the requestData
public String getFileName(String requestData) {
String[] result = new String[10];
result = requestData.split(“[?]”);
return result[0];
}
// save the info into the map《S,S》
public Map《String, String》 getValues(String requestData) {
Map《String, String》 values = new HashMap《String, String》();
String[] result = new String[10];
String regex = “[&=]”;
if (requestData.contains(“?”)) {
result = requestData.split(“[?]”);
String data_List = result[1];
result = data_List.split(regex);
for (int i = 0; i 《 result.length - 1; i += 2) {
values.put(result[i], result[i + 1]);
}
return values;
} else {
result = requestData.split(regex);
for (int i = 0; i 《 result.length - 1; i += 2) {
values.put(result[i], result[i + 1]);
}
return values;
}
}
}
整个项目结构
评论