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

📄 sevpost.java

📁 实现http的多项功能
💻 JAVA
字号:

import java.io.*;
import java.net.*;
import java.util.*;

public class sevpost {
	public static void main(String argv[]) throws Exception {
		int port=6789;
		ServerSocket listenSocket = null;
		try{
			listenSocket = new ServerSocket(port); 
        	for (;;) {
        		Socket connectionSocket = listenSocket.accept(); // 接受客户机的连接请求
        		System.out.println("New connection accepted " +
            			 connectionSocket.getInetAddress() +
            			 ":" + connectionSocket.getPort());
        		new Connection(connectionSocket).start();
        		} 
        	} catch(Exception e)
        	{
        		System.out.println(e);
        		}
        	}
	}
  	 class Connection extends Thread {
  		String requestMessageLine;
  		String fileName;
  		Socket connectionclient;
  		public Connection(Socket c1){
  			connectionclient=c1;
  			}
  		public void run() 
  		{ 
  			try {
  				DataOutputStream outToClient = new DataOutputStream(connectionclient.getOutputStream());
  				BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionclient.getInputStream()));
  				requestMessageLine=inFromClient.readLine(); // 读取Web浏览器提交的请求信息
  	        	StringTokenizer tokenizedLine = new StringTokenizer(requestMessageLine);
  	        	System.out.println(requestMessageLine);    
  	        	String s = tokenizedLine.nextToken();
  				System.out.println("Received:"+requestMessageLine); 
  				if (s.equals("GET")) { // 如果是GET请求 
  					fileName = tokenizedLine.nextToken();
  					if (fileName.startsWith("/") == true)
  	        			 fileName = fileName.substring(1);  					
  					System.out.println(fileName+" requested.");
  					File file=new File(fileName);
  					if (file.exists()) {
  						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");
  						outToClient.writeBytes("Content-Length:"+numOfBytes+"\r\n");
  						outToClient.writeBytes("\r\n");
  						outToClient.write(fileInBytes,0,numOfBytes);
  						} 
  					else { // 文件不存在时
  						String notfound="<html><head><title>Not Found</title></head><body><h1>Error 404-file not found</h1></body></html>";
  						outToClient.writeBytes("HTTP/1.0 404 no found"); 
  						outToClient.writeBytes("Content_Type:text/html"); 
  						outToClient.writeBytes("Content_Length:"+notfound.length()+2); 
  						outToClient.writeBytes(""); 
  						outToClient.writeBytes(notfound); 
  						} 
  					connectionclient.close(); 
  					} 
  				else{ 
  	        		 if(s.equals("POST")){ 
  	        			 StringTokenizer token;
  	        			 String post=null;
  	               		 fileName = tokenizedLine.nextToken();
  	            		 String str = null;
  	            		 char buff[] = new char[1024];
  	            		 int sizeOfBuff;
  	            		 sizeOfBuff = inFromClient.read(buff);
  	            		 str = new String(buff,0,sizeOfBuff);
  	            		 if (fileName.startsWith("/") == true)
  	            			 fileName = fileName.substring(1);
  	            		 System.out.println(fileName);
  	            		 System.out.println(str);
  	            		 File file = new File(fileName);        		 
  	            		 if (file.exists()) {
  	            			 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");
  	            			 outToClient.writeBytes("Content-Length:"+numOfBytes+"\r\n");
  	            			 outToClient.writeBytes("\r\n");
  	            			 outToClient.write(fileInBytes,0,numOfBytes);
  	            			 }
  	            		 token = new StringTokenizer(str, "\n\n");
  	            		 while(token.hasMoreTokens())
  	            			 post = token.nextToken();
  	            		 namevalue(post);              		 
  	            		 }
  	        		 else {
  	        			 System.out.println("Bad Request Message");
  	           			 String worr="<html><head><title>Not 	Found</title></head><body><h1>Error 400 ; Failure of POST testing !</h1></body></html>"; 
  	        	         outToClient.writeBytes("Content_Type:text/html"); 
  	        	         outToClient.writeBytes("Content_Length:"+worr.length()+2); 
  	        	         outToClient.writeBytes(""); 
  	        	         outToClient.writeBytes(worr); 
  	        	         }
  	        		connectionclient.close();
  	        		 }
  				} catch(Exception e)
  	        		 {
  					System.out.println(e);
  					} 
  				}
  		private static void namevalue(String str){
  			String userName , value;
  			StringTokenizer token1 , token2;
  			token1 = new StringTokenizer(str,"&");
  			while (token1.hasMoreTokens()){
  				token2 = new StringTokenizer(token1.nextToken(),"=");
  				userName = token2.nextToken();
  				if(token2.hasMoreTokens()){
  					value = token2.nextToken();
  				}
  				else{
  					value = "<null>";
  				}
  				System.out.println( "POST得到的数据: "+userName+"\t\t"+value+"\n");
  			}
  			}
  		}

⌨️ 快捷键说明

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