- package com.itcast.tomcateV2;<div class="blockcode"><blockquote>package com.itcast.tomcateV2;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Properties;
- import java.util.Set;
- /*
- * 接受客户端发送过来的请求,判断客户端要请求的是哪个资源,将这个资源发送到客户端
- *
- */
- public class TestServer {
- public static final String WEB_ROOT =
- System.getProperty("user.dir") +"\\" + "webroot";
- // 代表客户端要请求资源的路径
- public static String url = "";
- //public static void startUp(String[] args) {
- public static void main(String[] args) {
-
- try {
-
- // 建立不本机器的ServerSocket,监听本机器的80端口
- ServerSocket serverSocket = new ServerSocket(9999);
- boolean shutdown = false;
- while (!shutdown) {
- Socket socket = serverSocket.accept();
- InputStream input = socket.getInputStream();
- OutputStream output = socket.getOutputStream();
- //localhost:9999/demo.html localhost:9999/aa
- parse(input);
- //demo.html demo.jpg aa GET /demo http/1.1
- if(url.indexOf(".")!=-1){
- sendStaticResource(output);
- }else{
- sendDynamicResource(output);
- }
-
-
- input.close();
- input=null;
-
- output.close();
- output=null;
-
- socket.close();
- socket=null;
-
- //System.out.println("url.........."+url);
-
- if(url.equals("/SHUTDOWN")){
- shutdown=true;
- }
-
-
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void parse(InputStream input) throws IOException {
- StringBuffer content = new StringBuffer(2048);
- int i;
- byte[] buffer = new byte[2048];
- // 读取客户端发送过来的数据,将数据读取到字节数组buffer中.i代表读取数据量的大小
- i = input.read(buffer);
- System.out.println(buffer);
- for (int j = 0; j < i; j++) {
- content.append((char) buffer[j]);
- }
- // 打印读取到的内容
- System.out.print(content.toString());
- // 截取客户端要请求的资源路径
- String uri = parseUri(content.toString());
-
- // 赋值给本类中静态成员
- url = uri;
- System.out.println("uri..............:"+uri);
- }
- // 截取客户端请求资源的路径
- public static String parseUri(String requestString) {
- int index1, index2;
- index1 = requestString.indexOf(' ');
- if (index1 != -1) {
- index2 = requestString.indexOf(' ', index1 + 1);
- if (index2 > index1)
- return requestString.substring(index1+2, index2);
- }
- return null;
- }
- public static void sendStaticResource(OutputStream output)
- throws IOException {
- byte[] bytes = new byte[2048];
- FileInputStream fis = null;
- try {
- File file = new File(TestServer.WEB_ROOT, url);
- if (file.exists()) {
- output.write("HTTP/1.1 200 OK\n".getBytes());
- output.write("Server: Apache-Coyote/1.1\n".getBytes());
- output.write("Content-Type: text/html;charset=UTF-8\n"
- .getBytes());
- output.write("\n".getBytes());
- fis = new FileInputStream(file);
- int ch = fis.read(bytes, 0, 2048);
- while (ch != -1) {
- output.write(bytes, 0, ch);
- ch = fis.read(bytes, 0, 2048);
- }
- } else {
- // file not found
- String errorMessage = "HTTP/1.1 404 File Not Found\r\n"
- + "Content-Type: text/html\r\n"
- + "Content-Length: 23\r\n" + "\r\n"
- + "<h1>File Not Found</h1>";
- output.write(errorMessage.getBytes());
- }
- } catch (Exception e) {
- // thrown if cannot instantiate a File object
- e.printStackTrace();
- System.out.println(e.toString());
- } finally {
- if (fis != null)
- fis.close();
- }
- }
- public static Map readConf() throws Exception{
- Map m=new HashMap();
-
- Properties p=new Properties();
- p.load(new FileInputStream(TestServer.WEB_ROOT+"\\conf.properties"));
-
- Set s=p.keySet();
-
- Iterator ii=s.iterator();
- while(ii.hasNext()){
- String k=(String)ii.next();
- String v=(String)p.get(k);
- m.put(k, v);
- }
-
- return m;
- }
-
- public static void sendDynamicResource(OutputStream ops) throws Exception{
- Map m=readConf();
- if(m.size()>0){
- Set s=m.keySet();
- Iterator ii=s.iterator();
- while(ii.hasNext()){
- String k=(String)ii.next();
- if(k.contains("url")){
- String k_v=(String)m.get(k);
- if(url.equals(k_v)){
- String classStr=(String)m.get(k_v);
- Class cc=Class.forName(classStr);
- Server sss=(Server)cc.newInstance();
- sss.server(ops);
- }
-
- }
- }
- }
- }
-
- }
复制代码
import java.io.IOException;
import java.io.OutputStream;
/*
* 抽取得能力
*/
public interface Server {
public void init();
public void server(OutputStream ops)throws IOException;
public void destroy();
}
|
|