用java 寫的一個簡單的 web 服務器(適合新手練練)

用java寫的一個web,效果如下圖;總結一下遇到的問題:

        

    1. 端口被佔用,可以嘗試下換端口;

 

        2. 個別電腦裝的軟件類型太多,有時候接收不到端口, 寫對了不出效果別糾結在一臺電腦,本人深受其害;

 

 

        3.保證開發過程的有效性,就要做到不斷測試,測試很重要. 如果不測試一口氣寫了一大堆,可能一個小報錯都可能讓你疑惑 很久;

 

        以下是代碼:

 

 

          在你的項目裏建立一個包叫webroot  ,然後建立一個

 

         名爲index.html的文件,在文件裏丟以下代碼:

 

          

<html>
 

              <head>
 

                  <title>服務器</title>
 

              </head>
 

               <body>
 

                    <div align="center">123456789</div>
 

              </body>
 

          </html>    


     

再往下就是你的class類的代碼:

              


 

/**
 * 
 */
package Web.server;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Administrator
 *
 */
public class WebServer {

 private static final int post = 8124;
 private static final String uri = System.clearProperty("user.dir")+File.separator+"webroot"+File.separator;

 /**
  * @param args
  */
 
 public WebServer(){
  System.out.println("WebServer()------star");
  System.out.println("url------------------->"+uri);
  System.out.println("File--------------->"+File.separator);
  try {
   ServerSocket listener=new ServerSocket(post);
   System.out.println("listener---服務器正在運行端口----------------->"+listener.getLocalPort());
   while(true){
    System.out.println("判斷是否進入循環");
    Socket socket=listener.accept();
    System.out.println("socket------------------->"+socket.getLocalPort());
    //new PrintWriter(socket.getOutputStream(),true).println("你好! 世界!!");
    //BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    //System.out.println("in--------------------------->"+in);
    //輸出流
    PrintStream out = new PrintStream(socket.getOutputStream(),true);
    //設置路徑
    File fileuri=new File(uri+"index.html");
    System.out.println("fileuri------------------------------>"+fileuri);
    //設置文件類型
    String contentType=null;
    contentType="text/html;charset=GBK";
    if(!fileuri.exists()){       //如果不存在此文件
     System.out.println("沒有找到這個文件");
    }else{
     System.out.println("找到這個文件");
     /*out.println("HTTP/1.0 200 OK");
     out.println(contentType);
     out.println();*/
    }
    
    FileInputStream fis=null;
    fis=new FileInputStream(fileuri);
    System.out.println("fis------------------------->"+fis);
    byte data[];
    data = new byte[fis.available()];
    System.out.println("data------------------>"+data);
    fis.read(data);
    out.write(data);
    out.close();
    if(fis!=null){
     fis.close();
    }
    
    socket.close();
   }
   
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 /*public void run(){
  System.out.println("run()--------------start");
 }*/
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
   new WebServer();
 }

}


 

 

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章