JAVA编写简单的http服务器

JAVA编写简单的Http服务器:

  1 package server;
  2 import java.io.ByteArrayOutputStream;
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.IOException;
  6 import java.io.InputStream;
  7 import java.io.InterruptedIOException;
  8 import java.io.OutputStream;
  9 import java.net.ServerSocket;
 10 import java.net.Socket;
 11 
 12 public class HttpServer
 13 {
 14     public static void main(String[] args) throws IOException 
 15     {
 16         int port = 80;
 17         new HttpServer().start(port);
 18     }
 19     
 20     public void start(int port) throws IOException 
 21     {
 22         @SuppressWarnings("resource")
 23         ServerSocket server = new ServerSocket(port);
 24         System.out.println("Server has started at port: " + port);
 25         while(true)
 26         {
 27             Socket client = server.accept();
 28             Thread thread = new Thread(new Services(client));
 29             thread.start();
 30         }
 31     }
 32     
 33     class Services implements Runnable
 34     {
 35         Socket client;
 36         
 37         public Services(Socket client)
 38         {
 39             this.client = client;
 40         }
 41         
 42         private byte[] getResource(String resource) throws IOException
 43         {
 44             File file = new File(resource);
 45             FileInputStream fis = new FileInputStream(file);
 46             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 47             byte[] buffer = new byte[1000];
 48             int read;
 49             while((read = fis.read(buffer)) != -1)
 50             {
 51                 baos.write(buffer, 0, read);
 52             }
 53             fis.close();
 54             baos.close();
 55             return baos.toByteArray();
 56         }
 57         
 58         private String getHead(String url)
 59         {
 60             //使用\\对.进行正则表达式中的转义
 61             String[] arr = url.split("\\.");
 62             String fileType = arr[arr.length - 1];
 63             if(fileType.equals("html"))
 64             {
 65                 return "HTTP/1.0200OK\n" + "Content-Type:text/html\n" + "Server:myserver\n\n";
 66             }
 67             else if(fileType.equals("jpg") || fileType.equals("gif") || fileType.equals("png"))
 68             {
 69                 return "HTTP/1.0200OK\n" + "Content-Type:image/jpeg\n" + "Server:myserver\n\n";
 70             }
 71             else
 72             {
 73                 return null;
 74             }
 75         }
 76         
 77         @Override
 78         public void run()
 79         {
 80             try
 81             {
 82                 InputStream is = client.getInputStream();
 83                 OutputStream os = client.getOutputStream();
 84                 client.setSoTimeout(500);
 85                 String url = "";
 86                 int state = 0;
 87                 while(true)
 88                 {
 89                     int readInt = is.read();
 90                     char c = (char)readInt;
 91                     boolean space = Character.isWhitespace(c);
 92                     switch (state)
 93                     {
 94                     case 0:
 95                         if(space) continue;
 96                         else state = 1;
 97                     case 1:
 98                         if(space)
 99                         {
100                             state = 2;
101                             continue;
102                         }
103                         else
104                         {
105                             continue;
106                         }
107                     case 2:
108                         if(space) continue;
109                         else state = 3;
110                     case 3:
111                         if(space) break;
112                         else 
113                         {
114                             url += c;
115                             continue;
116                         }
117                     }
118                     break;
119                 }
120                 while(true)
121                 {
122                     try
123                     {
124                         @SuppressWarnings("unused")
125                         int readInt = is.read();
126                     } 
127                     catch(InterruptedIOException e)
128                     {
129                         byte[] data = getResource("webapp" + url);
130                         os.write(getHead(url).getBytes("utf-8"));
131                         os.write(data);
132                         os.close();
133                         break;
134                     }
135                 }
136             } 
137             catch(IOException e)
138             {
139                 e.printStackTrace();
140             }
141         }
142     }
143 }

测试所用网页:

 1 <html>
 2     <head>
 3         <title>
 4             A famous hero, hxy!
 5         </title>
 6     </head>
 7     <body>
 8         <h1 align="center">hxy's home page</h1>
 9         <h2 align="center">hxy's home page</h2>
10         <h3 align="center">hxy's home page</h3>
11         <h4 align="center">hxy's home page</h4>
12         <h5 align="center">hxy's home page</h5>
13     </body>
14 </html>

代码下载:https://github.com/SplayHuo/JavaProject

posted @ 2016-05-12 20:54  hxy_has_been_used  阅读(511)  评论(0编辑  收藏  举报