Java HTTP Redirector

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

public class Redirector {

	private static final Logger logger = Logger.getLogger("Redirector");

	private final int port;
	private final String newSite;

	public Redirector(String newSite, int port) {
		this.port = port;
		this.newSite = newSite;
	}

	public void start() {
		try (ServerSocket server = new ServerSocket(port)) {
			logger.info("Redirecting connections on port " + server.getLocalPort() + " to " + newSite);

			while (true) {
				try {
					Socket s = server.accept();
					Thread t = new RedirectThread(s);
					t.start();
				} catch (IOException ex) {
					logger.warning("Exception accepting connection");
				} catch (RuntimeException ex) {
					logger.log(Level.SEVERE, "Unexpected error", ex);
				}
			}
		} catch (BindException ex) {
			logger.log(Level.SEVERE, "Could not start server.", ex);
		} catch (IOException ex) {
			logger.log(Level.SEVERE, "Error opening server socket", ex);
		}
	}

	private class RedirectThread extends Thread {

		private final Socket connection;

		RedirectThread(Socket s) {
			this.connection = s;
		}

		public void run() {
			try {
				Writer out = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "US-ASCII"));
				Reader in = new InputStreamReader(new BufferedInputStream(connection.getInputStream()));

				// read the first line only; that's all we need
				StringBuilder request = new StringBuilder(80);
				while (true) {
					int c = in.read();
					if (c == '\r' || c == '\n' || c == -1)
						break;
					request.append((char) c);
				}

				String get = request.toString();
				String[] pieces = get.split("\\w*");
				String theFile = pieces[1];

				// If this is HTTP/1.0 or later send a MIME header
				if (get.indexOf("HTTP") != -1) {
					out.write("HTTP/1.0 302 FOUND\r\n");
					Date now = new Date();
					out.write("Date: " + now + "\r\n");
					out.write("Server: Redirector 1.1\r\n");
					out.write("Location: " + newSite + theFile + "\r\n");
					out.write("Content-type: text/html\r\n\r\n");
					out.flush();
				}
				// Not all browsers support redirection so we need to
				// produce HTML that says where the document has moved to.
				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();
				logger.log(Level.INFO, "Redirected " + connection.getRemoteSocketAddress());
			} catch (IOException ex) {
				logger.log(Level.WARNING, "Error talking to " + connection.getRemoteSocketAddress(), ex);
			} finally {
				try {
					connection.close();
				} catch (IOException ex) {
				}
			}
		}
	}

	public static void main(String[] args) {
		int thePort;
		String theSite;
		theSite = "http://www.163.com";
		thePort = 8080;
		Redirector redirector = new Redirector(theSite, thePort);
		redirector.start();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值