📄 server.java
字号:
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
public Server() throws Exception {
//the server socket
ServerSocket listenSocket = new ServerSocket(8005);
//open the socket for client connecting with.
while (true) {
// the client socket
Socket connectionSocket = listenSocket.accept();
System.out.println("Accepting Connection...\n");
// reader and writer with client and file
FileInputStream inFromFile = null;
BufferedReader inFromClient = null;
DataOutputStream outToClient = null;
// response the request
try {
while (true) {
inFromClient = new BufferedReader(new InputStreamReader(
connectionSocket.getInputStream()));
outToClient = new DataOutputStream(connectionSocket.getOutputStream());
System.out.println("Waiting for command...");
String cmd = inFromClient.readLine();
System.out.println("The request for: " + cmd);
StringTokenizer token = new StringTokenizer(cmd);
if (token.countTokens() < 2) {
System.out.println("Command style error!");
outToClient.writeBytes("HTTP/1.1 400 Bad Request\n\r");
break;
}
else {
String method = token.nextToken();
String filename = token.nextToken();
//Make sure that the filename is correct.
if (filename.charAt(0) != '/') {
System.out.println("Filename command error!");
outToClient
.writeBytes("HTTP/1.1 400 Bad Request\n\r");
break;
}
// filename = filename.toUpperCase();
filename = (filename.equals("/") ? "INDEX.HTM"
: filename.substring(1));
File file = new File(filename);
if (!file.exists()) {
System.out.println("File not existed!");
outToClient
.writeBytes("HTTP/1.1 404 Not Found\n\r");
break;
}
//Response the right format request. Identify the file tpye and write it to the client.
System.out.println("Now respond the request...");
inFromFile = new FileInputStream(filename);
byte[] by = new byte[(int) file.length()];
inFromFile.read(by);
//response to get method
if( method.toLowerCase().equals("get")){
if (filename.endsWith(".html")||filename.endsWith(".html")) {
outToClient.writeBytes("HTTP/1.0 200 OK" + "\n\r");
outToClient.writeBytes("Content-Type: txt/html"
+ "\n\r");
outToClient.writeBytes("Content-Length: "
+ file.length() + "\n\r\n\r");
}
else if (filename.endsWith(".jpg")||filename.endsWith(".jepg")){
outToClient.writeBytes("HTTP/1.0 200 OK" + "\n\r");
outToClient.writeBytes("Content-Type: image"
+ "\n\r");
}
outToClient.write(by);
outToClient.flush();
outToClient.writeBytes("\n\r");
break;
}
//response to put method
if(method.toLowerCase().equals("put")){
//I ignore this method and just print the fileName
System.out.println(filename);
}
//response when the method is incorrect
else {
System.out.println("Method error!");
outToClient
.writeBytes("HTTP/1.1 400 Bad Request\n\r");
break;
}
}
}//end while
} catch (Exception e) {
e.printStackTrace();
} finally {
// Close the reader and writer.
System.out.println("Closing Connection...\n");
try {
if (inFromFile != null)
inFromFile.close();
if (inFromClient != null)
inFromClient.close();
if (outToClient != null)
outToClient.close();
if (connectionSocket != null)
connectionSocket.close();
} catch (IOException e) {
}
}
}
}
public static void main(String[] args) {
try {
Server server = new Server();
} catch (Exception e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -