httpgethandler.java

来自「使用java实现的RFC1945 http1.0协议。所有面向对象设计严谨按照R」· Java 代码 · 共 66 行

JAVA
66
字号
/**
 * 
 */
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 + =
减小字号Ctrl + -
显示快捷键?