?? httprequesthandler1.java
字號:
package testHttp.httpserver;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.StringTokenizer;
/**
*
* <p>Title: HttpRequestHandler.java</p>
* <p>Description: </p>
* <p>Copyright:OnewaveInc Copyright (c) 2007</p>
* <p>Company: OnewaveInc</p>
* @author Zhengrw
* @version 3.0
*/
public class HttpRequestHandler1 implements Runnable {
final static String CRLF = "\r\n";
Socket socket;
InputStream input;
OutputStream output;
BufferedReader br;
// 構造方法
public HttpRequestHandler1(Socket socket) throws Exception {
this.socket = socket;
this.input = socket.getInputStream();
this.output = socket.getOutputStream();
this.br = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
}
// 實現(xiàn)Runnable 接口的run()方法
public void run() {
try {
processResquest0();
} catch (Exception e) {
System.out.println(e);
}
}
private void processResquest0() throws Exception {
String headerLine = br.readLine();
while(headerLine!=null){
System.out.println(headerLine);
headerLine = br.readLine();
// if (headerLine.equals(CRLF) || headerLine.equals(""))
// break;
}
}
private void processRequest() throws Exception {
while (true) {
//讀取并顯示W(wǎng)eb 瀏覽器提交的請求信息
String headerLine = br.readLine();
System.out.println("The client request is " + headerLine);
if (headerLine.equals(CRLF) || headerLine.equals(""))
break;
StringTokenizer s = new StringTokenizer(headerLine);
String temp = s.nextToken();
System.out.println(br.toString());
if (temp.equals("GET")) {
String fileName = s.nextToken();
fileName = "." + fileName;
// 打開所請求的文件
FileInputStream fis = null;
boolean fileExists = true;
try {
fis = new FileInputStream(fileName);
} catch (FileNotFoundException e) {
fileExists = false;
}
// 完成回應消息
String serverLine = "Server: a simple java httpServer";
String statusLine = null;
String contentTypeLine = null;
String entityBody = null;
String contentLengthLine = "error";
if (fileExists) {
statusLine = "HTTP/1.0 200 OK" + CRLF;
contentTypeLine = "Content-type: " + contentType(fileName)
+ CRLF;
contentLengthLine = "Content-Length: "
+ (new Integer(fis.available())).toString() + CRLF;
} else {
statusLine = "HTTP/1.0 404 Not Found" + CRLF;
contentTypeLine = "text/html";
entityBody = "<HTML>"
+ "<HEAD><TITLE>404 Not Found</TITLE></HEAD>"
+ "<BODY>404 Not Found"
+ "<br>usage:http://www.huoho.com:port/"
+ "fileName.html</BODY></HTML>";
}
// 發(fā)送到服務器信息
output.write(statusLine.getBytes());
output.write(serverLine.getBytes());
output.write(contentTypeLine.getBytes());
output.write(contentLengthLine.getBytes());
output.write(CRLF.getBytes());
// 發(fā)送信息內容
if (fileExists) {
sendBytes(fis, output);
fis.close();
} else {
output.write(entityBody.getBytes());
}
}
}
//關閉套接字和流
try {
output.close();
br.close();
socket.close();
} catch (Exception e) {
}
}
private static void sendBytes(FileInputStream fis, OutputStream os)
throws Exception {
// 創(chuàng)建一個 1K buffer
byte[] buffer = new byte[1024];
int bytes = 0;
// 將文件輸出到套接字輸出流中
while ((bytes = fis.read(buffer)) != -1) {
os.write(buffer, 0, bytes);
}
}
private static String contentType(String fileName) {
if (fileName.endsWith(".htm") || fileName.endsWith(".html")) {
return "text/html";
}
return "fileName";
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -