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

📄 server.java

📁 A Web proxy server listens to requests, then retrieves the requested files from other Web servers an
💻 JAVA
字号:
/*
 * Created on 2008-3-13
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author taobinbin 053732
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import java.io.*;
import java.net.*;
import java.util.*;

public class Server {
	
	private static final int socketPort = 5678;
	
	public Server(){}
	
	/*
	 *this method is used to answer the users' "get"; 
	 */
	public void methodOfGet(StringTokenizer lineToRead,DataOutputStream dataToOut){
		
		String fileName;/*string to storage the filename*/
		
		fileName = lineToRead.nextToken();
		/*first judge if the filename is start with"/"*/
		if(fileName.startsWith("/") == true){
			
			try{
				
				fileName = "C:\\www\\test\\"+fileName.substring(1);/*get the direct path od the file*/
				
				System.out.println(fileName);
				
				File fileOfMine = new File(fileName);
				
				int num = (int)fileOfMine.length();/*get the length of the file*/
				FileInputStream fileIn = new FileInputStream(fileName);

				byte[] byteOfFile = new byte[num];
				fileIn.read(byteOfFile);/*read the file to the char array*/
				
				dataToOut.writeBytes("HTTP/1.0 200 Document Follows\r\n");/*output the http header*/
				/*judge the kind of the file (jpg html gif)*/
				if(fileName.endsWith(".jpg"))
					dataToOut.writeBytes("Content-Type: image/jpeg\r\n");
				if(fileName.endsWith(".html")||fileName.endsWith(".hml"))
					dataToOut.writeBytes("Content-Type: txt/html\r\n");
				if(fileName.endsWith(".gif"))
					dataToOut.writeBytes("Content-Type: image/gif\r\n");
				
				dataToOut.writeBytes("Content-length:"+num+"\r\n");
				dataToOut.writeBytes("\r\n");
				dataToOut.write(byteOfFile,0,num);
			}catch(FileNotFoundException fnfe){  /*catch the error of no-file*/
			
				System.err.println("The specified file is not exist!");
				fnfe.printStackTrace();
			}catch(IOException ioe){
			
				System.err.println("IO erro:");
				ioe.printStackTrace();
			}
		}
	}
	/*the mian function of the class server*/
	public static void main(String argv[])throws IOException{
	
		Server server = new Server();
		String getFromClinet = "";
		ServerSocket welcomeSocket = null;
		
		try{
		
			welcomeSocket = new ServerSocket(socketPort);/*get a server socket*/
			System.out.println("Socket");
		}catch(IOException ioe){
			
			System.err.println("IO erro:");/*give the information of the error*/
			ioe.printStackTrace();
		}
		while(true){
			
			Socket acceptSocket = null;
		
			try{
			
				acceptSocket = welcomeSocket.accept();/*when the client give a message get it */
				System.out.println("WelcomSocket.accept()");
				
			}catch(IOException ioe){
			
				System.err.println("IO erro:");
				ioe.printStackTrace();
			}
			/*buffer to get message*/
			BufferedReader getBuffered = new BufferedReader(new InputStreamReader(acceptSocket.getInputStream()));
			/*stream to output message*/
			DataOutputStream dataToOut = new DataOutputStream(acceptSocket.getOutputStream());
			
			try{
			
				getFromClinet = getBuffered.readLine();
			}catch(IOException ioe){
			
				System.err.println("IO erro:");
				ioe.printStackTrace();
			}
			/*use the class StringTokenizer to divid the string*/
			StringTokenizer lineTokenizer = new StringTokenizer(getFromClinet);
			
			/*judge if the request is "get"*/
			if(lineTokenizer.nextToken().equals("GET")){
			
				/*use the sub function to answer the request*/
				server.methodOfGet(lineTokenizer,dataToOut);
			}else{
				System.out.println("Bad Request!");
			}
			acceptSocket.close();
		}
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -