📄 liukui.txt
字号:
/*
* Created on 2009-3-31
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author liukui
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.Timer;
public class Server {
/*standard input stream*/
private static BufferedReader stdIn =
new BufferedReader(new InputStreamReader(System.in));
/*standard output stream*/
private static PrintWriter stdOut =
new PrintWriter(System.out,true);
/*the default port number*/
static final int DEFAULT_PORT = 8000;
/*the port number request by the client*/
private int port = 0;
/*a identifier shows the request from the client is legal*/
static final int SUCCESS = 200;
/*a identifier shows the request from the client is illegal*/
static final int FAILED = 400;
/*a variable shows the request is legal or not*/
static String responseState = 200 + " OK";
/*a client socket*/
Socket client;
/*a server socket*/
ServerSocket server;
/*the server's name*/
static String hostname = null;
/*the response time */
static Date responseTime;
/*the code name of the erro when the client send a bad request*/
static String ERRO = "";
/**
*
*/
public Server(String hostname) {
try {
this.hostname = hostname;
server = new ServerSocket(DEFAULT_PORT);
stdOut.println(hostname + "Server Running...");
client = server.accept();
} catch (Exception e) {
e.toString();
}
}
/**
*
*/
public void response(long fileLength,String fileType,boolean containFile) throws Exception {
DataOutputStream outToClient = new DataOutputStream(client.getOutputStream());
String sendToClient = null;
//long fileLength = file.length();
sendToClient = "HTTP/1.0 " + responseState + "\n"
+"Server: " + hostname + "\n"
+"Date: " + responseTime + "\n"
+"Content-Type: " + fileType + "\n"
+"Content-Length: " + fileLength + "\n"
+"Expires: " + new Date() + "\n"//the time the response end
+ERRO+"wode"+"Via: " + hostname + DEFAULT_PORT
+"Connection: close";
//sendToClient += "<html><head>wode</head></html>";
outToClient.writeBytes(sendToClient);
}
public static void main(String[] args) throws Exception {
String fileType = null;
String newUrl = null;
long fileLength = 0;
boolean containFile = false;
if(args.length != 1) {
stdOut.println("error");
}
else {
Server s = new Server(args[0]);
try {
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(s.client.getInputStream()));
//the request from the client
String command = inFromClient.readLine();
StringTokenizer request= new StringTokenizer(command);
command = request.nextToken();
responseState = FAILED + " Bad Request";
responseTime = new Date();
ERRO = "X_" + hostname +"ERR_INVALID_REQ 0\n";
if(command.equals("GET")) {
String url = "";
//the path of the file which the client request
if(request.hasMoreTokens()) {
url = request.nextToken();
if(url.startsWith("/")) {
request = new StringTokenizer(url,"/");
while(request.hasMoreTokens()) {
newUrl = newUrl + "\\" + request.nextToken();
}
if(newUrl.endsWith(".html")) {
fileType = "text/html";
responseTime = new Date();
containFile = true;
} else if(newUrl.endsWith(".jpg")) {
fileType = "image/jpeg";
responseTime = new Date();
containFile = true;
} else if(newUrl.endsWith(".gif")) {
fileType = "image/gif";
responseTime = new Date();
containFile = true;
} else if(newUrl.endsWith(".txt")) {
fileType = "txt";
responseTime = new Date();
containFile = true;
}
}
}
url = hostname + "\\" + newUrl;
try {
File file = new File(url);
fileLength = file.length();
if(containFile == true) {
responseState = SUCCESS + " OK";
responseTime = new Date();
ERRO = "";
}
} catch(Exception e) {
}
}
s.response(fileLength,fileType,containFile);
} catch (Exception e) {
e.toString();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -