码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA编写http服务器

时间:2016-05-13 12:47:05      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

回宿舍补完。

 

  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         ServerSocket server = new ServerSocket(port);
 23         System.out.println("Server starts at port: " + port);
 24         while(true)
 25         {
 26             Socket client = server.accept();
 27             ServerThread thread = new ServerThread(client);
 28             thread.start();
 29         }
 30     }
 31     
 32     class ServerThread extends Thread
 33     {
 34         Socket client;
 35         
 36         public ServerThread(Socket client)
 37         {
 38             this.client = client;
 39         }
 40         
 41         public byte[] getFileByte(String fileName) throws IOException
 42         {
 43             File file = new File(fileName);
 44             FileInputStream fis = new FileInputStream(file);
 45             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 46             byte[] buffer = new byte[1000];
 47             int read;
 48             while((read = fis.read(buffer)) != -1)
 49             {
 50                 baos.write(buffer, 0, read);
 51             }
 52             fis.close();
 53             baos.close();
 54             return baos.toByteArray();
 55         }
 56         
 57         private String getQueryResource(String url)
 58         {
 59             String standardUrl = null;
 60             int index = url.indexOf(‘?‘);
 61             if(index != -1)
 62             {
 63                 standardUrl = url.substring(0, index);
 64             }
 65             else
 66             {
 67                 standardUrl = url;
 68             }
 69             return standardUrl;
 70         }
 71         
 72         private String getHead(String queryResource)
 73         {
 74             int index = queryResource.lastIndexOf(‘/‘);
 75             String fileName = queryResource.substring(index + 1);
 76             String[] fileTypes = fileName.split("\\.");
 77             String fileType = fileTypes[fileTypes.length - 1];
 78             if(fileType.equals("html"))
 79             {
 80                 return "HTTP/1.0200OK\n" + "Content-Type:text/html\n" + "Server:myserver\n\n";
 81             }
 82             else if(fileType.equals("jpg") || fileType.equals("gif") || fileType.equals("png"))
 83             {
 84                 return "HTTP/1.0200OK\n" + "Content-Type:image/jpeg\n" + "Server:myserver\n\n";
 85             }
 86             else
 87             {
 88                 return null;
 89             }
 90         }
 91         
 92         @Override
 93         public void run()
 94         {
 95             try
 96             {
 97                 InputStream is = client.getInputStream();
 98                 int readInt;
 99                 char c;
100                 byte[] buff = new byte[1000];
101                 OutputStream os = client.getOutputStream();
102                 client.setSoTimeout(500);
103                 byte[] data = null;
104                 String cmd = "", queryUrl = "", queryResource, head;
105                 int state = 0;
106                 while(true)
107                 {
108                     readInt = is.read();
109                     c = (char)readInt;
110                     boolean space = Character.isWhitespace(c);
111                     switch (state)
112                     {
113                     case 0:
114                         if(space) continue;
115                         else state = 1;
116                     case 1:
117                         if(space)
118                         {
119                             state = 2;
120                             continue;
121                         }
122                         else
123                         {
124                             cmd += c;
125                             continue;
126                         }
127                     case 2:
128                         if(space) continue;
129                         else state = 3;
130                     case 3:
131                         if(space) break;
132                         else 
133                         {
134                             queryUrl += c;
135                             continue;
136                         }
137                     }
138                     break;
139                 }
140                 queryResource = getQueryResource(queryUrl);
141                 head = getHead(queryResource);
142                 while(true)
143                 {
144                     try
145                     {
146                         if((readInt = is.read(buff)) > 0)
147                         {
148                             System.out.write(buff);
149                         }
150                         else if (readInt < 0)
151                         {
152                             break;
153                         }
154                     } 
155                     catch(InterruptedIOException e)
156                     {
157                         data = getFileByte("webapp" + queryResource);
158                     }
159                     if(data != null)
160                     {
161                         os.write(head.getBytes("utf-8"));
162                         os.write(data);
163                         os.close();
164                         break;
165                     }
166                 }
167             } 
168             catch(IOException e)
169             {
170                 e.printStackTrace();
171             }
172         }
173     }
174 }

 

JAVA编写http服务器

标签:

原文地址:http://www.cnblogs.com/huoxiayu/p/5487290.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!