?? request.java
字號:
import java.io.*;
/*用戶請求類*/
public final class Request {
private String rootdir; //站點文檔根目錄
private String requestDoc; //用戶請求的文檔
/**
* 構造器
* 第一個參數是用戶請求頭,用來獲取所要請求的文檔
* 第二個參數是默認主頁,如果請求頭里沒有指定具體某一頁面,則返回此默認主頁
* 第三個參數是站點文檔根目錄,讀取文檔時用來指定文檔路徑
*/
public Request(String requestHeader, String defaultPage, String rootdir) {
int index1 = requestHeader.indexOf("/");
int index2 = requestHeader.indexOf("HTTP");
String doc = requestHeader.substring(index1+1,index2-1);
if(doc.equals("")) {
requestDoc = defaultPage;
}else {
requestDoc = doc;
}
this.rootdir = rootdir;
}
/*獲取用戶請求的文檔*/
public byte[] getDoc() {
File file = new File(rootdir + requestDoc);
System.out.println("getDoc file path = " + file.getPath());
if(file.exists()) { //判斷請求的文檔是否存在
try {
BufferedInputStream reader = new BufferedInputStream(new FileInputStream(file));
byte[] doc = new byte[reader.available()];
reader.read(doc);
reader.close();
return doc;
}catch(IOException ex) {
ex.printStackTrace();
}
}
return null;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -