a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > JSP > 正文

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

2024-09-05 00:23:09
字體:
供稿:網(wǎng)友

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

實例代碼:

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("//"))             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);   } } 

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


注:相關(guān)教程知識閱讀請移步到JSP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产精品国产三级国产aⅴ无密码 | 天久久 | 九色在线视频 | 色婷婷综合久久久中文字幕 | 亚洲精品自拍 | 久久久一二三四 | 国产区 在线观看 | 国产精品久久久 | 成人av网站免费观看 | 亚洲精品自在在线观看 | 日韩超碰| 亚洲国产精品精华液com | 亚洲国产精品成人 | 狠狠se| 欧美日韩在线精品 | 国产精品美女久久久久久久久久久 | 国产精品一区二区久久 | 欧美日韩在线播放 | 日本视频在线观看 | 亚洲自拍电影 | 日韩免费不卡视频 | 国内精品久久精品 | 欧美一级在线观看 | 精品久久网站 | 色av综合在线 | 国产精品一区二区三区在线 | 日韩亚洲视频在线观看 | 久久综合色88 | 中文字幕在线看 | 理论片免费在线观看 | 国产精品www| 久久99久久98精品免观看软件 | 国产精品一区二区三区av | 欧美自拍视频在线 | 羞羞视频网| 特黄aaaaaa裸体视频 | 国产精品视频男人的天堂 | 先锋av资源在线 | 日韩免费视频一区二区 | 精品久久久久国产免费 | 最新中文字幕第一页 |