?? httpserver.java
字號:
package com.liuxin;
import java.io.*;
import java.net.*;
import java.util.*;
public class HttpServer {
public static void start() {
try {
ServerSocket ss = new ServerSocket(8080); // 服務(wù)端開啟一個監(jiān)聽端口;
while (true) {
/**
* 接受到別人發(fā)送的請求; 除非有人來連接,否則程序?qū)⒁恢弊枞谶@里;
*/
Socket sock = ss.accept();
System.out.println("=======start==========");
BufferedReader br = new BufferedReader(new InputStreamReader(
sock.getInputStream())); // 得到輸入流;
PrintStream ps = new PrintStream(sock.getOutputStream()); // 得到輸出流;
String[] sarry = null;
String stime = "";
String str = br.readLine(); // 以行的形式讀入數(shù)據(jù);
while (str != null && str.equals("") == false) { // 取得以"GET"和"If-Modified-Since:"開頭的字符串中的子串;
if (str.startsWith("GET") && str.endsWith("HTTP/1.1")) {
sarry = str.split(" ");
} else if (str.startsWith("If-Modified-Since:")) {
stime = str.substring(24, str.length()); // 取得文件的最后修改時間;
}else{}
str = br.readLine();
}
System.out.println(stime + " stime");
File f = new File("d:" + sarry[1]); // 這樣可以訪問同一臺服務(wù)器上的不同文件(包括網(wǎng)頁、TXT文本文件)
String s_modify_time = new Date(f.lastModified()).toGMTString();// 得到當(dāng)前文件的最后修改時間;
if(s_modify_time.length() == 23){ //確保服務(wù)器時間和客戶端的時間格式一致;
s_modify_time = "0" + s_modify_time;
}else{
s_modify_time = s_modify_time;
}
System.out.println(s_modify_time + "s_modify_time");
if (f.exists()) {
if(stime.equals(s_modify_time)){ // ????
ps.println("HTTP/1.1 304 NOT MODIFYED");
ps.println();
}else if(stime.equals("")){
ps.println("HTTP/1.1 200 OK");
// ps.println("Last-Modified:" + s_modify_time); // 錯誤情況;
ps.println("Last-Modified:" + new Date(f.lastModified())); // 發(fā)送文件的最后修改時間;
ps.println(); // 格式要求;
FileInputStream fis = new FileInputStream(f);
/**
* 記住byte數(shù)組的定義方式;這里只是定義,為什么下邊可以直接從byte數(shù)組中讀取文件內(nèi)容,
* 文件內(nèi)容時怎么放到數(shù)組中去的 ???
*/
byte[] buff = new byte[(int) f.length()];
fis.read(buff); // 把服務(wù)器資源index.html中的信息讀進(jìn)來;
ps.write(buff); // 把服務(wù)器資源index.html中的信息發(fā)送到客戶端去;
fis.close();
}else{
ps.println("意外情況!");
}
} else if (f.exists() == false) {
System.out.println("找不到文件!");
ps.println("HTTP/1.1 404 NOT FOUND");
}
ps.flush();
ps.close();
sock.close();
}
}catch(BindException e){
System.out.println("服務(wù)器已經(jīng)啟動 !");
} catch (IOException e) {
e.printStackTrace();
}
}
// =======================================================================
public static void main(String[] args) {
HttpServer hs = new HttpServer();
hs.start();
}
}
// ==================================================================================
// String arrStr2 = arrStr.substring(1, arrStr.length()-1); //將arrStr中頭、尾的括號去掉;
/*
* //創(chuàng)建Cookie Cookie cookie = new Cookie("name", "zhangsan");
*
* //設(shè)置Cookie的超時時間 cookie.setMaxAge(24 60 6060);
*
* //把Cookie發(fā)送到客戶端 response.addCookie(cookie);
*
* //得到客戶端發(fā)送的Cookie Cookie [] cookies = request.getCookies(); for(int i=0; i
* <cookies.length; i++) { Cookie temp = cookies[i]; String key =
* temp.getName(); String value = temp.getValue(); }
*/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -