A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© STARlove 中级黑马   /  2015-9-23 13:52  /  292 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package com.itcast.tomcateV2;<div class="blockcode"><blockquote>package com.itcast.tomcateV2;

  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;
  9. import java.util.HashMap;
  10. import java.util.Iterator;
  11. import java.util.Map;
  12. import java.util.Properties;
  13. import java.util.Set;
  14. /*
  15. * 接受客户端发送过来的请求,判断客户端要请求的是哪个资源,将这个资源发送到客户端
  16. *
  17. */
  18. public class TestServer {

  19.         public static final String WEB_ROOT =
  20.                         System.getProperty("user.dir") +"\\"  + "webroot";
  21.         // 代表客户端要请求资源的路径
  22.         public static String url = "";

  23.         //public static void startUp(String[] args) {
  24.         public static void main(String[] args) {
  25.                
  26.                 try {
  27.                        
  28.                         // 建立不本机器的ServerSocket,监听本机器的80端口
  29.                         ServerSocket serverSocket = new ServerSocket(9999);

  30.                         boolean shutdown = false;

  31.                         while (!shutdown) {
  32.                                 Socket socket = serverSocket.accept();
  33.                                 InputStream input = socket.getInputStream();
  34.                                 OutputStream output = socket.getOutputStream();
  35.                                 //localhost:9999/demo.html   localhost:9999/aa
  36.                                 parse(input);
  37.                                 //demo.html  demo.jpg   aa  GET /demo  http/1.1
  38.                                 if(url.indexOf(".")!=-1){
  39.                                         sendStaticResource(output);       
  40.                                 }else{
  41.                                         sendDynamicResource(output);
  42.                                 }
  43.                                
  44.                                
  45.                                 input.close();
  46.                                 input=null;
  47.                                
  48.                                 output.close();
  49.                                 output=null;
  50.                                
  51.                                 socket.close();
  52.                                 socket=null;
  53.                                
  54.                                 //System.out.println("url.........."+url);
  55.                                
  56.                                 if(url.equals("/SHUTDOWN")){
  57.                                         shutdown=true;
  58.                                 }
  59.                                
  60.                                
  61.                         }

  62.                 } catch (Exception e) {
  63.                         e.printStackTrace();
  64.                 }

  65.         }

  66.         public static void parse(InputStream input) throws IOException {
  67.                 StringBuffer content = new StringBuffer(2048);
  68.                 int i;

  69.                 byte[] buffer = new byte[2048];
  70.                 // 读取客户端发送过来的数据,将数据读取到字节数组buffer中.i代表读取数据量的大小
  71.                 i = input.read(buffer);
  72.                 System.out.println(buffer);

  73.                 for (int j = 0; j < i; j++) {
  74.                         content.append((char) buffer[j]);
  75.                 }
  76.                 // 打印读取到的内容
  77.                 System.out.print(content.toString());
  78.                 // 截取客户端要请求的资源路径
  79.                 String uri = parseUri(content.toString());
  80.                
  81.                 // 赋值给本类中静态成员
  82.                 url = uri;
  83.                 System.out.println("uri..............:"+uri);
  84.         }

  85.         // 截取客户端请求资源的路径
  86.         public static String parseUri(String requestString) {
  87.                 int index1, index2;
  88.                 index1 = requestString.indexOf(' ');
  89.                 if (index1 != -1) {
  90.                         index2 = requestString.indexOf(' ', index1 + 1);
  91.                         if (index2 > index1)
  92.                                 return requestString.substring(index1+2, index2);
  93.                 }
  94.                 return null;
  95.         }

  96.         public static void sendStaticResource(OutputStream output)
  97.                         throws IOException {

  98.                 byte[] bytes = new byte[2048];
  99.                 FileInputStream fis = null;
  100.                 try {
  101.                         File file = new File(TestServer.WEB_ROOT, url);
  102.                         if (file.exists()) {
  103.                                 output.write("HTTP/1.1 200 OK\n".getBytes());
  104.                                 output.write("Server: Apache-Coyote/1.1\n".getBytes());
  105.                                 output.write("Content-Type: text/html;charset=UTF-8\n"
  106.                                                 .getBytes());
  107.                                 output.write("\n".getBytes());

  108.                                 fis = new FileInputStream(file);
  109.                                 int ch = fis.read(bytes, 0, 2048);
  110.                                 while (ch != -1) {
  111.                                         output.write(bytes, 0, ch);
  112.                                         ch = fis.read(bytes, 0, 2048);
  113.                                 }
  114.                         } else {
  115.                                 // file not found
  116.                                 String errorMessage = "HTTP/1.1 404 File Not Found\r\n"
  117.                                                 + "Content-Type: text/html\r\n"
  118.                                                 + "Content-Length: 23\r\n" + "\r\n"
  119.                                                 + "<h1>File Not Found</h1>";
  120.                                 output.write(errorMessage.getBytes());
  121.                         }
  122.                 } catch (Exception e) {
  123.                         // thrown if cannot instantiate a File object
  124.                         e.printStackTrace();
  125.                         System.out.println(e.toString());
  126.                 } finally {
  127.                         if (fis != null)
  128.                                 fis.close();
  129.                 }
  130.         }

  131.         public static Map readConf() throws Exception{
  132.                 Map m=new HashMap();
  133.                
  134.                 Properties p=new Properties();
  135.                 p.load(new FileInputStream(TestServer.WEB_ROOT+"\\conf.properties"));
  136.                
  137.                 Set s=p.keySet();
  138.                
  139.                 Iterator ii=s.iterator();
  140.                 while(ii.hasNext()){
  141.                         String k=(String)ii.next();
  142.                         String v=(String)p.get(k);
  143.                         m.put(k, v);
  144.                 }
  145.                
  146.                 return m;
  147.         }
  148.        
  149.         public static void sendDynamicResource(OutputStream ops) throws Exception{
  150.                 Map m=readConf();
  151.                 if(m.size()>0){
  152.                         Set s=m.keySet();
  153.                         Iterator ii=s.iterator();
  154.                         while(ii.hasNext()){
  155.                                 String k=(String)ii.next();
  156.                                 if(k.contains("url")){
  157.                                         String k_v=(String)m.get(k);
  158.                                         if(url.equals(k_v)){
  159.                                                 String classStr=(String)m.get(k_v);
  160.                                                 Class cc=Class.forName(classStr);
  161.                                                 Server sss=(Server)cc.newInstance();
  162.                                                 sss.server(ops);
  163.                                         }
  164.                                        
  165.                                 }
  166.                         }
  167.                 }               
  168.         }
  169.        
  170. }
复制代码



import java.io.IOException;
import java.io.OutputStream;

/*
* 抽取得能力
*/
public interface Server {
        public void init();
        public void server(OutputStream ops)throws IOException;
        public void destroy();
}

2 个回复

倒序浏览
tomcat/     
回复 使用道具 举报

我们老师手速太快。。停不下来。。每次都会写成cate....{:3_65:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马