⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.java

📁 ssd8 exercise1.rar the answer, I hope to help all of you are welcome to download
💻 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 + -