📄 httpgethandler.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 + -