日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

基于Jave的Web服務(wù)工作機(jī)制(7)_Windows教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!
sendStaticResource 方法是非常簡單的。它首先傳遞父路徑和子路徑給File類的構(gòu)造器,從而對(duì)java.io.File類進(jìn)行了實(shí)例化。

  File file = new File(HttpServer.WEB_ROOT, request.getUri());
  然后它檢查文件是否存在。如果存在,sendStaticResource 方法通過傳遞File對(duì)象來構(gòu)造一個(gè)java.io.FileInputStream對(duì)象。然后調(diào)用FileInputStream 的read方法,將字節(jié)流寫如到OutputStream輸出。注意這種情況下, 靜態(tài)資源的內(nèi)容也被作為原始數(shù)據(jù)被發(fā)送給了瀏覽器。

if (file.exists()) {
  fis  = new FileInputStream(file);
  int ch = fis.read(bytes, 0, BUFFER_SIZE);

  while (ch != -1) {
    output.write(bytes, 0, ch);
    ch = fis.read(bytes, 0, BUFFER_SIZE);
  }
}

  如果這個(gè)文件不存在,sendStaticResource 方法發(fā)送一個(gè)錯(cuò)誤消息給瀏覽器。

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());

  編譯和運(yùn)行應(yīng)用程序

  為了編譯和運(yùn)行應(yīng)用,你首先需要解壓包含本文應(yīng)用程序的.zip文件。你解壓的目錄成為工作目錄(working directory),它有三個(gè)子目錄: src/, classes/, 和 lib/。 要編譯應(yīng)用程序需要在工作目錄輸入如下語句:

  javac -d . src/ex01/pyrmont/*.java
  這個(gè)-d 選項(xiàng)參數(shù)將結(jié)果寫到當(dāng)前目錄,而不是src/ 目錄。

  要運(yùn)行應(yīng)用程序,在工作目錄中輸入如下語句:

  java ex01.pyrmont.HttpServer
  要測試你的應(yīng)用程序,打開瀏覽器,在地址欄中輸入如下URL:

  http://localhost:8080/index.html
  你將可以看到瀏覽器中顯示的index.html 頁面。

  Figure 1. The output from the web server

  在控制臺(tái)(Console),你能看到如下內(nèi)容:

  GET /index.html HTTP/1.1
  Accept: */*
  Accept-Language: en-us
  Accept-Encoding: gzip, deflate
  User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; windows 98)
  Host: localhost:8080
  Connection: Keep-Alive

  GET /images/logo.gif HTTP/1.1
  Accept: */*
  Referer: http://localhost:8080/index.html
  Accept-Language: en-us
  Accept-Encoding: gzip, deflate
  User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; windows 98)
  Host: localhost:8080
  Connection: Keep-Alive

  概要總結(jié)

  在本文中,你了解了一個(gè)簡單的Web服務(wù)器的工作機(jī)制。本文附帶的應(yīng)用程序源代碼只包含三個(gè)類,但并不是所有的都有用。盡管如此,它還是能被作為一種很好的學(xué)習(xí)工具為我們服務(wù)。

來源:網(wǎng)絡(luò)搜集//所屬分類:Windows教程/更新時(shí)間:2013-04-15
相關(guān)Windows教程