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

📄 httprequest.java

📁 Java版Web Sever
💻 JAVA
字号:
import java.net.*;
import java.io.*;
import java.util.*;

import javax.swing.*;

final class HttpRequest implements Runnable
{
	protected PrintWriter print;
	protected BufferedOutputStream bufout;
	protected BufferedReader bufread;
	protected File path;
	protected Socket socket;
	protected JTextArea message;
	
	public HttpRequest(Socket socket, JTextArea message,String path) throws Exception 
	{
		this.socket = socket;
		this.message = message;
		this.path = new File(path).getCanonicalFile();
	}
	
	public void run()
	{
		try
		{
			bufread = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			bufout = new BufferedOutputStream(socket.getOutputStream());
			print = new PrintWriter(new OutputStreamWriter(bufout));
			message.append("IP: " + socket.getInetAddress().getHostAddress() + "\n"); //显示请求IP
			message.append("Port: "+ socket.getPort() + "\n"); //显示请求端口
			String line = bufread.readLine();
			line = line.replaceAll("%20", "`"); //替换空格
			line = java.net.URLDecoder.decode(line,"UTF-8"); //汉字解码
			message.append(line + "\n\n"); //显示请求信息
			socket.shutdownInput();

			if(line == null)
			{
				socket.close();
				return;
			}
			if(line.toUpperCase().startsWith("GET"))
			{
				StringTokenizer tokens = new StringTokenizer(line," ?"); //命令分解
				tokens.nextToken(); //返回下一个标记
				String request = tokens.nextToken();
				
				String name;
				if(request.startsWith("/")||request.startsWith("\\")) name = this.path+request;
				else name = this.path+File.separator+request;
				name = name.replaceAll("`", " "); //解码空格
				File file = new File(name).getCanonicalFile(); //获取抽象路径名的规范形式
				
				if(!file.getAbsolutePath().startsWith(this.path.getAbsolutePath())){
					print.println("HTTP/1.0 403 Forbidden");
					print.println();
				}
				else if(!file.exists()){
					print.println("HTTP/1.0 404 File Not Found");
					print.println();
				}
				else if(!file.canRead()){
					print.println("HTTP/1.0 403 Forbidden");
					print.println();
				}
				else if(file.isDirectory()) senddirectory(bufout,print,file,request);
				else sendfile(bufout,print,file.getAbsolutePath());
			}
			else{
				print.println("HTTP/1.0 501 Not Implemented");
				print.println();	
			}
			print.flush();
			bufout.flush();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		try{
			socket.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	protected void sendfile(BufferedOutputStream bufout,PrintWriter print,String filename)throws Exception
	{
		try
		{
			java.io.BufferedInputStream bis = new java.io.BufferedInputStream(new FileInputStream(filename));
			byte[] data = new byte[10*1024];
			int read = bis.read(data);
			
			print.println("HTTP/1.0 200 Okay");
			print.println();
			print.flush();
			bufout.flush();
			
			while(read != -1)
			{
				bufout.write(data,0,read);
				read = bis.read(data);
			}
			bufout.flush();
		}
		catch(Exception e)
		{
			print.flush();
			bufout.flush();
		}
	}
	
	protected void senddirectory(BufferedOutputStream bufout,PrintWriter print,File dir,String request)throws Exception
	{
		try
		{
			print.println("HTTP/1.0 200 Okay");
			print.println();
			print.flush();
			
			print.print("<html><head><title>");
			print.print(request);
			print.print("</title></head><body><h1>");
			print.print(request);
			print.println("</h1><table border=\"0\">");
			
			File[] contents = dir.listFiles();
			
			for(int i=0;i<contents.length;i++)
			{
				if(!contents[i].isDirectory()) continue;
				print.print("<tr>");
				print.print("<td><a href=\"");
				print.print(request);
				print.print(contents[i].getName());
				print.print("/\">(Dir) ");
				print.print(contents[i].getName());
				print.print("</a></td>");
				print.println("</tr>");
			}
			for(int i=0;i<contents.length;i++)
			{
				if(contents[i].isDirectory()) continue;
				print.print("<tr>");
				print.print("<td><a href=\"");
				print.print(request);
				print.print(contents[i].getName());
				print.print("\">(File) ");
				print.print(contents[i].getName());
				print.print("</a></td>");
				print.println("</tr>");
			}
			print.println("</table></body></html>");
			print.flush();
		}
		catch(Exception e)
		{
			print.flush();
			bufout.flush();
		}
	}
}

⌨️ 快捷键说明

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