新手入门-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
其中有/的为目录。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值