?? server.java
字號:
import java.io.*;
import java.net.*;
import java.util.*;
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);
/* Standard error stream */
private static PrintWriter stdErr = new PrintWriter(System.err, true);
private static String CRLF = "\r\n";
public static void main(String args[]) throws Exception {
/* Create a server socket to respond to the clients' responds. */
String request = "";
ServerSocket welcomeSocket = new ServerSocket(8000);
/* always ready to receive request */
while (true) {
/* Accept the client's socket connection. */
Socket connection = welcomeSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
request = in.readLine();
StringTokenizer st = new StringTokenizer(request);
String token = st.nextToken();
token = token.toUpperCase();
if (token.equals("GET")) {
String filename = st.nextToken();
if (filename.startsWith("/") == true)
filename = filename.substring(1);
filename = "C:\\" + filename;
//filename.toUpperCase();
/* Construct a FileInputStream object to read the file */
File file = new File(filename);
int numOfBytes = (int) file.length();
FileInputStream fileIn = new FileInputStream(filename);
byte fileBytes[] = new byte[numOfBytes];
fileIn.read(fileBytes, 0, numOfBytes);
/* judge the requested file's name */
out.writeBytes("HTTP/1.0 200 Document Follows" + CRLF);
filename.toUpperCase();
if (filename.endsWith(".JPG")) {
out.writeBytes("Content-type: image/jpeg" + CRLF);
} else if (filename.endsWith(".GIF")) {
out.writeBytes("Content-type: image/gif" + CRLF);
} else if (filename.endsWith(".HTM")
|| filename.endsWith(".HTML")) {
out.writeBytes("Content-type: docment/htm/html" + CRLF);
}
/* write to the client */
out.writeBytes("Content-Length: " + numOfBytes + CRLF + CRLF);
/* write file content to client */
out.write(fileBytes, 0, numOfBytes);
} else if (token == "PUT") {
} else {
}
in.close();
out.close();
connection.close();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -