新手入門-Java編的Web服務器

package WebServer;

import java.net.*;
import java.io.*;

class Process implements Runnable{
    Socket s;
    public Process(Socket s1){
        s=s1;
    }
    public void run() {
        try{
            PrintStream out=new PrintStream(s.getOutputStream());
            BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
            String info=in.readLine();
            System.out.println("now got "+info);
            out.println("HTTP/1.0 200 OK");
            out.println("MIME_version:1.0");
            out.println("Content_Type:text/html");
            /*
             * 瀏覽器請求形如 GET /T/1.HTML HTTP/1.1
             * sp1,sp2爲第一次和第二次出現空格的位置
             * filename爲從瀏覽器中提出文件路徑和名稱 如 T/1.HTML
             */
            int sp1=info.indexOf(' ');
            int sp2=info.indexOf(' ',sp1+1);
            String fileName=info.substring(sp1+2,sp2);
            if(fileName.equals("")||fileName.endsWith("/")){
                fileName+="index.html";
            }else if((sp1=fileName.indexOf(".htm"))!=-1){
                fileName=fileName.substring(0,sp1)+".html";
            }
            System.out.println("Sending "+fileName);
            File fi=new File(fileName);
          //判斷輸入的文件是否存在,不存在返回錯誤。
            if(!fi.exists()){
                fileName="error.html";
            }
            fi=new File(fileName);
            System.out.println("Sending "+fileName);
            InputStream fs=new FileInputStream(fi);
            int n=fs.available(); //n爲文件長度
            byte buf[]=new byte[1024];
            out.println("Content_Length: "+n);
            out.println("");
            while((n=fs.read(buf))>=0){
                out.write(buf,0,n);
               
            }
            out.close();
            s.close();
            in.close();
        }
        catch(IOException e){
            System.out.println("Exception: "+e);
        }
       
    }
   
}
public class MyWebServer {

    /**
     * @mc1035
     */
    public static void main(String[] args) {
        try{
            ServerSocket ss=new ServerSocket(80);
            System.out.println("Web Server OK");
            while(true){
                Socket s=ss.accept();
                Process p=new Process(s);
                Thread t=new Thread(p);
                t.start();
            }
        }catch(Exception e){
            System.out.println(e);
        }
       
    }

}
 本段程序主要實現了靜態web服務,功能是監聽80端口,從IE瀏覽器端獲得輸入的網頁文件,格式爲:http://127.0.0.1/test.html或http://127.0.0.1/
其目錄結構爲:
                    webserver:/
                                                 test.html
                                                 error.html
                                                 index.html

                                                 WebServer/
                                                                              MyWebServer.class
                                                                              Process.class
                                                                              MyWebServer.java
其中有/的爲目錄。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章