📄 threadserver.java
字号:
www.pudn.com > Ex 2.rar > ThreadServer.java
import java.io.*;
import java.net.*;
import java.util.*;
public class ThreadServer {
public static ServerSocket listenSocket;
public static void main(String argv[]) throws Exception {
final int port = 8080;
listenSocket = new ServerSocket(port);
Socket connectionSocket = null;
System.out.println("Welcome to the server!");
System.out.println(new Date());
System.out.println("The server is ready!");
System.out.println("Port is: " + port);
connectionSocket = listenSocket.accept();
Runnable run = new ThreadHttp(connectionSocket);
Thread t1 = new Thread(run);
t1.start();
} // end of main
} // end of class
class ThreadHttp implements Runnable {
public Socket connectionSocket;
public ThreadHttp(Socket ConnSocket) {
connectionSocket = ConnSocket;
}
public void run() {
String requestMessageLine;
String fileName = "";
StringTokenizer tokenizedLine;
// the request type,"PUT" or "GET"
String request = "";
// length of the data
String length = "";
// write the data to a file
FileOutputStream pout;
while(true) {
try {
DataOutputStream outToClient = new DataOutputStream(
connectionSocket.getOutputStream());
DataInputStream inFromClient = new DataInputStream(
connectionSocket.getInputStream());
requestMessageLine = inFromClient.readLine();
tokenizedLine = new StringTokenizer(requestMessageLine);
request = tokenizedLine.nextToken();
fileName = tokenizedLine.nextToken();
if(fileName.startsWith("/") == true)
fileName = fileName.substring(1);
if(request.equals("GET")) {
File file = new File(fileName);
int numOfBytes = (int)file.length();
FileInputStream inFile = new FileInputStream(fileName);
byte[] fileInBytes = new byte[numOfBytes];
inFile.read(fileInBytes);
outToClient.writeBytes(
"HTTP/1.0 200 Document Follows\r\n");
if(fileName.endsWith(".gif"))
outToClient.writeBytes("Content-Type: image/gif\r\n");
if(fileName.endsWith(".jpg"))
outToClient.writeBytes("Content-Type: image/jpg\r\n");
if(fileName.endsWith(".html"))
outToClient.writeBytes("Content-Type: text/html\r\n");
if(fileName.endsWith(".txt"))
outToClient.writeBytes("Content-Type: text/plain\r\n");
outToClient.writeBytes("Content-Length: " + numOfBytes + "\r\n");
outToClient.writeBytes("\r\n");
outToClient.write(fileInBytes,0,numOfBytes);
outToClient.flush();
}
else if(request.equals("PUT")) {
do {
requestMessageLine = inFromClient.readLine().trim();
// the data starts with "/r/n"
if(requestMessageLine.equals(""))
break;
else if(requestMessageLine.startsWith("Content-Length:")) {
length = requestMessageLine.substring(16);
}
} while(true);
// accept the data and write it to a file
int len = Integer.parseInt(length);
byte[] buffer = new byte[len];
inFromClient.read(buffer,0,len);
// write the data to a file
pout= new FileOutputStream(fileName);
pout.write(buffer);
pout.close();
}
else {
System.out.println("Bad Request Message");
}
}
catch (FileNotFoundException e) {
}
catch (IOException ex) {
System.err.println("IO error");
}
} // end of while loop
} // end of run method
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -