⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpserver.java

📁 WEB服务器
💻 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); // 服务端开启一个监听端口;

			while (true) {
				/**
				 * 接受到别人发送的请求; 除非有人来连接,否则程序将一直阻塞在这里;
				 */
				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();                               // 以行的形式读入数据;
				
				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]);                   // 这样可以访问同一台服务器上的不同文件(包括网页、TXT文本文件)
				String s_modify_time = new Date(f.lastModified()).toGMTString();// 得到当前文件的最后修改时间;
				
				if(s_modify_time.length() == 23){                     //确保服务器时间和客户端的时间格式一致;
					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())); // 发送文件的最后修改时间;
						ps.println();                                              // 格式要求;
						FileInputStream fis = new FileInputStream(f);
						/**
						 * 记住byte数组的定义方式;这里只是定义,为什么下边可以直接从byte数组中读取文件内容,
						 * 文件内容时怎么放到数组中去的 ???
						 */
						byte[] buff = new byte[(int) f.length()];
						fis.read(buff);                             // 把服务器资源index.html中的信息读进来;
						ps.write(buff);                             // 把服务器资源index.html中的信息发送到客户端去;
						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("服务器已经启动 !");
		} 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中头、尾的括号去掉;
/*
 * //创建Cookie Cookie cookie = new Cookie("name", "zhangsan");
 * 
 * //设置Cookie的超时时间 cookie.setMaxAge(24 60 6060);
 * 
 * //把Cookie发送到客户端 response.addCookie(cookie);
 * 
 * //得到客户端发送的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(); }
 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -