---
title: "简单的web服务器"
author: "yujiawei"
date: "2009-06-17T09:26:47Z"
tags: ["CSS", "Java", "JavaScript", "Socket", "Web"]
slug: "web"
draft: false
lastmod: "2009-06-17T09:26:47Z"
id: 409731
url: "https://yujiawei.iteye.com/blog/409731"
---

Server.java \[code="java"] package cn.tuoxie007.webserver; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.text.SimpleDateFormat; import java.util.Date; public class Server { private boolean run = true; public Server(int port) { //start server try { ServerSocket ss = new ServerSocket(port); while (run) { //get a client Socket s = ss.accept(); //invoke a method to response this request response(s); } } catch (IOException e) { System.err.println("server eroor :" + e.getMessage()); } } byte\[] readFile(File f){ try { return StringUtil.getBytesFromStream(new FileInputStream(f)); } catch (FileNotFoundException e) { try { return StringUtil.getBytesFromStream(new FileInputStream("www/404.html")); } catch (FileNotFoundException e1) { } catch (IOException e1) { } } catch (IOException e) { } return new byte\[0]; } int getFileLength(File f){ return readFile(f).length; } static byte\[] FILE\_NOT\_FOUND\_HEAD = "HTTP/1.1 404 Not Found\\n".getBytes(); static byte\[] REPONSE\_OK = "HTTP/1.1 200 OK\\n".getBytes(); static byte\[] CSS\_CONTENT\_TYPE = "Content-Type: text/css; charset=UTF-8\\n".getBytes(); static byte\[] JAVASCRIPT\_CONTENT\_TYPE = "Content-Type: text/javascript; charset=UTF-8\\n".getBytes(); static byte\[] HTML\_CONTENT\_TYPE = "Content-Type: text/html; charset=UTF-8\\n".getBytes(); static byte\[] JPEG\_CONTENT\_TYPE = "Content-Type: image/jpeg\\n".getBytes(); static byte\[] GIF\_CONTENT\_TYPE = "Content-Type: image/gif\\n".getBytes(); static byte\[] IMPORTANT\_DATA = new byte\[]{ 83, 101, 114, 118, 101, 114, 58, 32, 115, 105, 109, 112, 108, 101, 32, 115, 116, 97, 116, 105, 99, 32, 119, 101, 98, 32, 115, 101, 114, 118, 101, 114, 32, 98, 121, 32, 116, 117, 111, 120, 105, 101, 48, 48, 55, 10}; /\** * response client * @param s \*/ private void response(Socket s) { try { BufferedReader br = new BufferedReader(new InputStreamReader(s .getInputStream())); String firstLine = br.readLine(); if (firstLine.startsWith("GET") && firstLine.contains("HTTP/")) { String\[] parts = firstLine.split("\\\\s+"); if (parts.length &gt; 1) { String requestFile = parts\[1]; File f = getFile(requestFile); OutputStream out = s.getOutputStream(); if(!f.exists()){// file not found out.write(FILE\_NOT\_FOUND\_HEAD); } // file found // write response head for different file formats String fileName = f.getName(); out.write(REPONSE\_OK); out.write(IMPORTANT\_DATA); out.write(("Content-Length: " + getFileLength(f) + "\\n").getBytes()); if(fileName.endsWith("css")){ out.write(CSS\_CONTENT\_TYPE); }else if(fileName.endsWith("js")){ out.write(JAVASCRIPT\_CONTENT\_TYPE); }else if(fileName.endsWith("jpg")){ out.write("Accept-Ranges: bytes\\n".getBytes()); out.write(JPEG\_CONTENT\_TYPE); }else if(fileName.endsWith("gif")){ out.write("Accept-Ranges: bytes\\n".getBytes()); out.write(GIF\_CONTENT\_TYPE); }else{ out.write(REPONSE\_OK); out.write(HTML\_CONTENT\_TYPE); } out.write("\\n".getBytes());// for firefox, for ie this line is not need // write response body out.write(readFile(f)); // log this request log(s.getInetAddress().getHostAddress(), f.getAbsolutePath().split("www")\[1]); } } } catch (IOException e) { }finally{ if(s != null){ try { s.close(); } catch (IOException e) { } } } } private void log(String ip, String file) { // TODO you can record this request here // write this message to disk yourself okay? I just print them System.out.println("\[INFO] " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ", client:" + ip + ", request file:" + file); } /\** * get local file * @param file * @return * @author xuke \*/ private File getFile(String file) { if(file.equals("/")){ file += "index.html"; }else{ String\[] parts = file.split("/"); String endFile = parts\[parts.length-1]; if(endFile.endsWith("/")){ file += "index.html"; }else if(!endFile.contains(".")){ file += "/index.html"; } } return new File(new File("www"), file); } /\** * close server * @author xuke \*/ public void shutdown() { this.run = false; } /\** * Main * @param args \*/ public static void main(String\[] args) { new Server(80); } } \[/code] StringUtil.java \[code="java"] package cn.tuoxie007.webserver; import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.util.LinkedList; import java.util.List; /\** * string util fetch content from a stream thought about charset, * or codec a bytes and so on * @author xuke *  
\* [welcome to autor's home page](http://tuoxie007.cn) * \*/ public class StringUtil { public static final int READ\_BUFFER = 10240; /\** * merge the list of byte array to a singal byte array * @param bufList * @return \*/ public static byte\[] getBytesFromByteList(List bufList){ int size = 0; for(int i=0; i getListFromStream(InputStream is) throws IOException{ DataInputStream dis = new DataInputStream(new BufferedInputStream(is)); int read = 0; List bufList = new LinkedList(); byte\[] buf = new byte\[READ\_BUFFER]; while (true) { int len = dis.read(buf, read, READ\_BUFFER - read); if (len == -1) { byte\[] buff = new byte\[read]; for(int i=0; i bufList = new LinkedList(); byte\[] buf = new byte\[READ\_BUFFER]; while (true) { int len = dis.read(buf, read, READ\_BUFFER - read); if (len == -1) { bufList.add(buf); break; } read += len; if (read == READ\_BUFFER) { bufList.add(buf); buf = new byte\[READ\_BUFFER]; read = 0; } } byte\[] bytes = new byte\[bufList.size() &gt; 1 ? bufList.size() * READ\_BUFFER + read : read]; int off = 0; for (int i = 0; i &lt; bufList.size(); i++) { byte\[] b = bufList.get(i); int len = Math.min(bytes.length - i * READ\_BUFFER, READ\_BUFFER); for (int j = 0; j &lt; len; j++) { bytes\[off++] = b\[j]; } } return bytes; } } \[/code]