您好,登錄后才能下訂單哦!
除了服務(wù)器類,還包括請求類和響應(yīng)類
請求類:獲取客戶的HTTP請求,分析客戶所需要的文件
響應(yīng)類:獲得用戶請求后將用戶需要的文件讀出,添加上HTTP應(yīng)答頭。發(fā)送給客戶端。
服務(wù)器處理類
package com.lp.app.webserver; import java.io.*; import java.net.*; //使用Socket創(chuàng)建一個WEB服務(wù)器,本程序是多線程系統(tǒng)以提高反應(yīng)速度。 class WebServer { public static String WEBROOT = "";//默認(rèn)目錄 public static String defaultPage = "index.htm";//默認(rèn)文件 public static void main (String [] args) throws IOException { System.out.println ("服務(wù)器啟動...\n"); //使用8080端口提供服務(wù) ServerSocket server = new ServerSocket (8080); while (true) { //阻塞,直到有客戶連接 Socket sk = server.accept (); System.out.println ("Accepting Connection...\n"); //啟動服務(wù)線程 new WebThread (sk).start (); } } } //使用線程,為多個客戶端服務(wù) class WebThread extends Thread { private Socket sk; WebThread (Socket sk) { this.sk = sk; } //線程體 public void run () { InputStream in = null; OutputStream out = null; try{ in = sk.getInputStream(); out = sk.getOutputStream(); //接收來自客戶端的請求。 Request rq = new Request(in); //解析客戶請求 String sURL = rq.parse(); System.out.println("sURL="+sURL); if(sURL.equals("/")) sURL = WebServer.defaultPage; Response rp = new Response(out); rp.Send(sURL); } catch (IOException e) { System.out.println (e.toString ()); } finally { System.out.println ("關(guān)閉連接...\n"); //最后釋放資源 try{ if (in != null) in.close (); if (out != null) out.close (); if (sk != null) sk.close (); } catch (IOException e) { } } } }
請求類
package com.lp.app.webserver; import java.io.*; import java.net.*; //獲取客戶的HTTP請求,分析客戶所需要的文件 public class Request{ InputStream in = null; //獲得輸入流。這是客戶的請求數(shù)據(jù)。 public Request(InputStream input){ this.in = input; } //解析客戶的請求 public String parse() { //從Socket讀取一組數(shù)據(jù) StringBuffer requestStr = new StringBuffer(2048); int i; byte[] buffer = new byte[2048]; try { i = in.read(buffer); } catch (IOException e) { e.printStackTrace(); i = -1; } for (int j=0; j<i; j++) { requestStr.append((char) buffer[j]); } System.out.print(requestStr.toString()); return getUri(requestStr.toString()); } //獲取URI信息字符 private String getUri(String requestString) { int index1, index2; index1 = requestString.indexOf(' '); if (index1 != -1) { index2 = requestString.indexOf(' ', index1 + 1); if (index2 > index1) return requestString.substring(index1 + 1, index2); } return null; } }
響應(yīng)類
package com.lp.app.webserver; import java.io.*; import java.net.*; //獲得用戶請求后將用戶需要的文件讀出,添加上HTTP應(yīng)答頭。發(fā)送給客戶端。 public class Response{ OutputStream out = null; //發(fā)送請求的文件 public void Send(String ref) throws IOException { byte[] bytes = new byte[2048]; FileInputStream fis = null; try { //構(gòu)造文件 File file = new File(WebServer.WEBROOT, ref); if (file.exists()) { //構(gòu)造輸入文件流 fis = new FileInputStream(file); int ch = fis.read(bytes, 0, 2048); //讀取文件 String sBody = new String(bytes,0); //構(gòu)造輸出信息 String sendMessage = "HTTP/1.1 200 OK\r\n" + "Content-Type: text/html\r\n" + "Content-Length: "+ch+"\r\n" + "\r\n" +sBody; //輸出文件 out.write(sendMessage.getBytes()); }else { // 找不到文件 String errorMessage = "HTTP/1.1 404 File Not Found\r\n" + "Content-Type: text/html\r\n" + "Content-Length: 23\r\n" + "\r\n" + "<h2>File Not Found</h2>"; out.write(errorMessage.getBytes()); } } catch (Exception e) { // 如不能實例化File對象,拋出異常。 System.out.println(e.toString() ); } finally { if (fis != null) fis.close(); } } //獲取輸出流 public Response(OutputStream output) { this.out = output; } }
以上這篇java使用socket實現(xiàn)一個多線程web服務(wù)器的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。