?? server.java
字號:
/**
* @author wenhuaxiao
* @see <code>Client.java</code>
* @version 1.0
*
*
*/
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
// The port number in static final type .
private static final int socketPort = 6789;
// Default constructor.
public Server (){ }
public void methodGet(StringTokenizer tokenizedLine, DataOutputStream outToClient) {
String fileName;
fileName = tokenizedLine.nextToken();
if(fileName.startsWith("/") == true) {
try {
fileName = "C:\\www\\test\\" + fileName.substring(1);
System.out.println(fileName);
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");
// Jugde the user's requested file's extended type name.
if(fileName.endsWith(".jpg"))
outToClient.writeBytes("Content-Type: image/jpeg\r\n");
if(fileName.endsWith(".html") || fileName.endsWith(".htm"))
outToClient.writeBytes("Content-Type: text/html\r\n");
if(fileName.endsWith(".gif"))
outToClient.writeBytes("Content-Type: image/gif\r\n");
// Write to the client.
outToClient.writeBytes("Content-length: " + numOfBytes + "\r\n");
outToClient.writeBytes("\r\n");
outToClient.write(fileInBytes, 0, numOfBytes);
} catch (FileNotFoundException fnfe){
System.err.println("The specified file is not exist!");
fnfe.printStackTrace();
} catch (IOException ioe){
System.err.println("IO erro: ");
ioe.printStackTrace();
}
}
}
public static void main(String argv[]) throws IOException {
// Create a new Server object.
Server server = new Server();
String clientSentence = "";
ServerSocket welcomeSocket = null;
try
{
// Create a server socket to respond to the clients' responds.
welcomeSocket = new ServerSocket(socketPort);
System.out.println("Socket");
} catch (IOException e) {
System.err.println("IO erro: ");
e.printStackTrace();
}
// Always run.
while(true) {
Socket connectionSocket = null;
try {
// Accept the client's socket connection.
connectionSocket = welcomeSocket.accept();
System.out.println("welcomeSocket.accept()");
}catch(IOException ioe) {
System.err.println("IO erro: ");
ioe.printStackTrace();
}
BufferedReader inFromClient =
new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
try {
// Read the client's request.
clientSentence = inFromClient.readLine();
} catch(IOException io) {
System.err.println("IO erro: ");
io.printStackTrace();
}
StringTokenizer tokenizedLine =
new StringTokenizer(clientSentence);
if(tokenizedLine.nextToken().equals("GET")) {
// If the request is a "GET" request, then deal with it.
server.methodGet(tokenizedLine, outToClient);
} else {
// If it is not a valid request, then print out the erro message.
System.out.println("Bad request!");
}
connectionSocket.close();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -