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

📄 httpgethandler.java

📁 RFC 1945 Http1.0协议实现。对协议进行了完整面向对象设计
💻 JAVA
字号:
/**
 * 
 */
package edu.sysu.http.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.util.Calendar;
import java.util.Date;

import edu.sysu.http.intf.HttpHandler;
import edu.sysu.http.util.HttpGrammarException;
import edu.sysu.http.util.HttpMediaTypeManager;
import edu.sysu.http.util.HttpResponseException;

/**
 * @author Administrator
 * 
 */
public class HttpGetHandler implements HttpHandler {

	/**
	 * 
	 */
	public HttpGetHandler() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	@SuppressWarnings("deprecation")
	@Override
	// /please check the host and port before handling
	public HttpResponse handle(HttpRequest request)
			throws HttpGrammarException, FileNotFoundException, IOException {
		// TODO Auto-generated method stub
		HttpResponse response = new HttpResponse();
		URI uri = request.getRequestLine().getUri();
		String path = uri.getPath();
		File file = null;
		if (path.endsWith("/")) {
			file = new File(HttpParameters.WebRoot + "index.html");
			response.setEntity(new HttpEntity(new FileInputStream(file.getAbsolutePath())));
			
			response.getEntity().setContentType(HttpMediaTypeManager.getInstance().getType(file));
		} else if (path != null) {
			String reqfile = path.substring(path.indexOf("/") + 1).replace("/",
					"\\");
			file = new File(HttpParameters.WebRoot + reqfile);
			response.setEntity(new HttpEntity(new FileInputStream(file.getAbsolutePath())));
			response.getEntity().setContentType(HttpMediaTypeManager.getInstance().getType(file));
		}
		return response;

	}
}

⌨️ 快捷键说明

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