婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > Java 實(shí)現(xiàn) web服務(wù)器的簡(jiǎn)單實(shí)例

Java 實(shí)現(xiàn) web服務(wù)器的簡(jiǎn)單實(shí)例

熱門標(biāo)簽:電話機(jī)器人需要使用網(wǎng)絡(luò)嗎 潤(rùn)滑油銷售電銷機(jī)器人 如何看懂地圖標(biāo)注點(diǎn) 自繪地圖標(biāo)注數(shù)據(jù) 海外圖書(shū)館地圖標(biāo)注點(diǎn) 電銷機(jī)器人免培訓(xùn) 南通通訊外呼系統(tǒng)產(chǎn)品介紹 外呼系統(tǒng)使用方法 給地圖標(biāo)注得傭金

Java 實(shí)現(xiàn) web服務(wù)器的簡(jiǎn)單實(shí)例

實(shí)例代碼:

import java.util.*; 
 
// Chapter 8, Listing 3 
public class WebServerDemo { 
  // Directory of HTML pages and other files 
  protected String docroot; 
  // Port number of web server 
  protected int port; 
  // Socket for the web server 
  protected ServerSocket ss; 
 
  // Handler for a HTTP request 
  class Handler extends Thread { 
    protected Socket socket; 
    protected PrintWriter pw; 
    protected BufferedOutputStream bos; 
    protected BufferedReader br; 
    protected File docroot; 
 
    public Handler(Socket _socket, String _docroot) throws Exception { 
      socket = _socket; 
      // Get the absolute directory of the filepath 
      docroot = new File(_docroot).getCanonicalFile(); 
    } 
 
    public void run() { 
      try { 
        // Prepare our readers and writers 
        br = new BufferedReader(new InputStreamReader( 
            socket.getInputStream())); 
        bos = new BufferedOutputStream(socket.getOutputStream()); 
        pw = new PrintWriter(new OutputStreamWriter(bos)); 
 
        // Read HTTP request from user (hopefully GET /file...... ) 
        String line = br.readLine(); 
 
        // Shutdown any further input 
        socket.shutdownInput(); 
 
        if (line == null) { 
          socket.close(); 
          return; 
        } 
        if (line.toUpperCase().startsWith("GET")) { 
          // Eliminate any trailing ? data, such as for a CGI GET 
          // request 
          StringTokenizer tokens = new StringTokenizer(line, " ?"); 
          tokens.nextToken(); 
          String req = tokens.nextToken(); 
 
          // If a path character / or / is not present, add it to the 
          // document root 
          // and then add the file request, to form a full filename 
          String name; 
          if (req.startsWith("/") || req.startsWith("http://")) 
            name = this.docroot + req; 
          else 
            name = this.docroot + File.separator + req; 
 
          // Get absolute file path 
          File file = new File(name).getCanonicalFile(); 
 
          // Check to see if request doesn't start with our document 
          // root .... 
          if (!file.getAbsolutePath().startsWith( 
              this.docroot.getAbsolutePath())) { 
            pw.println("HTTP/1.0 403 Forbidden"); 
            pw.println(); 
          } 
          // ... if it's missing ..... 
          else if (!file.exists()) { 
            pw.println("HTTP/1.0 404 File Not Found"); 
            pw.println(); 
          } 
          // ... if it can't be read for security reasons .... 
          else if (!file.canRead()) { 
            pw.println("HTTP/1.0 403 Forbidden"); 
            pw.println(); 
          } 
          // ... if its actually a directory, and not a file .... 
          else if (file.isDirectory()) { 
            sendDir(bos, pw, file, req); 
          } 
          // ... or if it's really a file 
          else { 
            sendFile(bos, pw, file.getAbsolutePath()); 
          } 
        } 
        // If not a GET request, the server will not support it 
        else { 
          pw.println("HTTP/1.0 501 Not Implemented"); 
          pw.println(); 
        } 
 
        pw.flush(); 
        bos.flush(); 
      } catch (Exception e) { 
        e.printStackTrace(); 
      } 
      try { 
        socket.close(); 
      } catch (Exception e) { 
        e.printStackTrace(); 
      } 
    } 
 
    protected void sendFile(BufferedOutputStream bos, PrintWriter pw, 
        String filename) throws Exception { 
      try { 
        java.io.BufferedInputStream bis = new java.io.BufferedInputStream( 
            new FileInputStream(filename)); 
        byte[] data = new byte[10 * 1024]; 
        int read = bis.read(data); 
 
        pw.println("HTTP/1.0 200 Okay"); 
        pw.println(); 
        pw.flush(); 
        bos.flush(); 
 
        while (read != -1) { 
          bos.write(data, 0, read); 
          read = bis.read(data); 
        } 
        bos.flush(); 
      } catch (Exception e) { 
        pw.flush(); 
        bos.flush(); 
      } 
    } 
 
    protected void sendDir(BufferedOutputStream bos, PrintWriter pw, 
        File dir, String req) throws Exception { 
      try { 
        pw.println("HTTP/1.0 200 Okay"); 
        pw.println(); 
        pw.flush(); 
 
        pw.print("html>head>title>Directory of "); 
        pw.print(req); 
        pw.print("/title>/head>body>h1>Directory of "); 
        pw.print(req); 
        pw.println("/h1>table border=/"0/">"); 
 
        File[] contents = dir.listFiles(); 
 
        for (int i = 0; i  contents.length; i++) { 
          pw.print("tr>"); 
          pw.print("td>a href="/" rel="external nofollow" rel="external nofollow" mce_href="/" rel="external nofollow" rel="external nofollow" ""); 
          pw.print(req); 
          pw.print(contents[i].getName()); 
          if (contents[i].isDirectory()) 
            pw.print("/"); 
          pw.print("/">"); 
          if (contents[i].isDirectory()) 
            pw.print("Dir -> "); 
          pw.print(contents[i].getName()); 
          pw.print("/a>/td>"); 
          pw.println("/tr>"); 
        } 
        pw.println("/table>/body>/html>"); 
        pw.flush(); 
      } catch (Exception e) { 
        pw.flush(); 
        bos.flush(); 
      } 
    } 
  } 
 
  // Check that a filepath has been specified and a port number 
  protected void parseParams(String[] args) throws Exception { 
    switch (args.length) { 
    case 1: 
    case 0: 
      System.err.println("Syntax: jvm> " + this.getClass().getName() 
          + " docroot port"); 
      System.exit(0); 
 
    default: 
      this.docroot = args[0]; 
      this.port = Integer.parseInt(args[1]); 
      break; 
    } 
  } 
 
  public WebServerDemo(String[] args) throws Exception { 
    System.out.println("Checking for paramters"); 
 
    // Check for command line parameters 
    parseParams(args); 
 
    System.out.print("Starting web server...... "); 
 
    // Create a new server socket 
    this.ss = new ServerSocket(this.port); 
 
    System.out.println("OK"); 
 
    for (;;) { 
      // Accept a new socket connection from our server socket 
      Socket accept = ss.accept(); 
 
      // Start a new handler instance to process the request 
      new Handler(accept, docroot).start(); 
    } 
  } 
 
  // Start an instance of the web server running 
  public static void main(String[] args) throws Exception { 
    WebServerDemo webServerDemo = new WebServerDemo(args); 
  } 
} 

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

您可能感興趣的文章:
  • java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的Web服務(wù)器實(shí)例解析
  • java 與web服務(wù)器鏈接的實(shí)例
  • AngularJS實(shí)現(xiàn)與Java Web服務(wù)器交互操作示例【附demo源碼下載】
  • 基于Java web服務(wù)器簡(jiǎn)單實(shí)現(xiàn)一個(gè)Servlet容器
  • 簡(jiǎn)單實(shí)現(xiàn)Java web服務(wù)器
  • Java實(shí)現(xiàn)簡(jiǎn)易Web服務(wù)器
  • Java中常見(jiàn)的5種WEB服務(wù)器介紹
  • Java Socket編程(五) 簡(jiǎn)單的WEB服務(wù)器
  • 搭建JavaWeb服務(wù)器步驟詳解

標(biāo)簽:樂(lè)山 貸款邀約 南京 黃石 大連 銅川 廣州 內(nèi)江

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Java 實(shí)現(xiàn) web服務(wù)器的簡(jiǎn)單實(shí)例》,本文關(guān)鍵詞  Java,實(shí)現(xiàn),web,服務(wù)器,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Java 實(shí)現(xiàn) web服務(wù)器的簡(jiǎn)單實(shí)例》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Java 實(shí)現(xiàn) web服務(wù)器的簡(jiǎn)單實(shí)例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 兴宁市| 河池市| 行唐县| 保亭| 富平县| 乃东县| 台中县| 红桥区| 额济纳旗| 丰城市| 仙居县| 贵阳市| 盐边县| 托里县| 深圳市| 南昌市| 绥德县| 桐乡市| 新野县| 龙陵县| 凤城市| 黄骅市| 雷山县| 万源市| 北川| 漾濞| 宁都县| 洞口县| 桂阳县| 安乡县| 桓台县| 浑源县| 丽水市| 雅江县| 莱芜市| 元朗区| 邵武市| 和硕县| 永善县| 万州区| 三河市|