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

📄 usewebredirector.java

📁 实现Web服务器重定向功能的java编程
💻 JAVA
字号:
import java.net.*;import java.io.*;import java.util.*;public class useWebRedirector implements Runnable {	//定义私有属性  	private int port;  	private String newSite;  	  	//构造器  	public useWebRedirector(String site, int port)   	{    		this.port = port;    		this.newSite = site;  	}	//建立线程并启动  	public void run()   	{        		try     		{    			//新建ServerSocket对象            			ServerSocket server = new ServerSocket(this.port);       			//输出重定向信息      			System.out.println("Redirecting connections on port "         		+ server.getLocalPort() + " to " + newSite);              			while (true)       			{                			try         			{        				//建立Socket对象          				Socket s = server.accept();          				//启动线程          				Thread t = new RedirectThread(s);          				t.start();        			}  // end try        			catch (IOException e)         			{ }              			} // end while          		} // end try    		catch (BindException e)     		{    			//输出异常信息      			System.err.println("Could not start server. Port Occupied");    		}             		catch (IOException e)     		{      			System.err.println(e);    		}           	}  // end run	class RedirectThread extends Thread 	{ 	 	           		private Socket connection;                	//线程构造器    		RedirectThread(Socket s)     		{      			this.connection = s;        		}            		public void run()     		{            			try       			{      				//缓冲输出流                			Writer out = new BufferedWriter(new OutputStreamWriter(         			connection.getOutputStream(), "ASCII"));        			//获得输入流        			Reader in = new InputStreamReader(new         			BufferedInputStream(connection.getInputStream()));                            			// 仅读取第一行        			StringBuffer request = new StringBuffer(80);        			while (true)         			{        				//从缓冲里读取输入流          				int c = in.read();          				//根据输入中止循环          				if (c == '\r' || c == '\n' || c == -1) break;          				//在字符串缓冲里增加字符          				request.append((char) c);        			}        			//获取缓冲字符串信息        			String get = request.toString();        			//判断空格位置        			int firstSpace = get.indexOf(' ');        			int secondSpace = get.indexOf(' ', firstSpace+1);        			//获取第一个空格和第二个空格之间的子串        			String theFile = get.substring(firstSpace+1, secondSpace);        			if (get.indexOf("HTTP/") != -1)         			{          				out.write("HTTP/4.0 302 FOUND\r\n");          				//新建Date对象          				Date now = new Date();          				out.write("Date: " + now + "\r\n");          				out.write("Server: Redirector 1.0\r\n");          				out.write("Location: " + newSite + theFile + "\r\n");                  				out.write("Content-type: text/html\r\n\r\n");                           				out.flush();                        			}        			// 不是所有的浏览器都支持重定向,所以需要创建HTML说明文献移到的位置        			out.write("<HTML><HEAD><TITLE>Document moved</TITLE></HEAD>\r\n");        			out.write("<BODY><H1>Document moved</H1>\r\n");        			out.write("The document " + theFile           			+ " has moved to\r\n<A HREF=\"" + newSite + theFile + "\">" + newSite          			+ theFile + "</A>.\r\n Please update your bookmarks<P>");        			out.write("</BODY></HTML>\r\n");        			out.flush();      			} // end try      			catch (IOException e)       			{ }      			finally       			{        			try         			{          				if (connection != null) connection.close();        			}        			catch (IOException e)         			{}        			}           		}  // end run	}//end RedirectThread	public static void main(String[] args) 	{		//设置局域变量    		int thePort;    		String theSite;        		try     		{      			theSite = args[0];      			// trim trailing slash      			if (theSite.endsWith("/"))       			{        			theSite = theSite.substring(0, theSite.length()-1);      			}    		}    		catch (Exception e)     		{    			//异常处理      			System.out.println("Usage: java Redirector http://www.newsite.com/ port");      			return;    		}        		try     		{      			thePort = Integer.parseInt(args[1]);    		}      		catch (Exception e)     		{      			thePort = 80;    		}        		//启动新的重定向线程    		Thread t = new Thread(new useWebRedirector(theSite, thePort));    		t.start();    	}  // end main} //end useWebRedirector

⌨️ 快捷键说明

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