熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java實現簡單web服務器

2022-06-13   來源: Java核心技術 

  該程序簡單實現web服務器目前僅支持html和gif/jpeg格式下面是代碼

  import javaio* ;

  import * ;

  import javautil* ;

  public class SimpleWebServer {

  public static void main(String argv[]) throws Exception {

  //Get the port as an argument

  // Also you could hard code the port to point to or any other port

  int port = (new Integer(argv[]))intValue();

  // Establish the listen socket

  ServerSocket socket = new ServerSocket(port);

  while(true){

  // Listen for a TCP connection request

  Socket connection = socketaccept();

  processRequest(connection);

  }

  }

  private static void processRequest(Socket connection) throws Exception {

  final String CRLF = \r\n;

  Socket socket = connection;

  // Get a reference to the sockets input and output streams

  InputStream is = socketgetInputStream();

  DataOutputStream os = new DataOutputStream(socketgetOutputStream());

  // Set up input stream filters

  BufferedReader br = new BufferedReader(new InputStreamReader(is));

  // Get the request line of the HTTP request message

  String requestLine = brreadLine();

  // Extract the filename from the request line

  StringTokenizer tokens = new StringTokenizer(requestLine);

  tokensnextToken(); // skip over the method which should be GET

  String fileName = tokensnextToken();

  // Prepend a so that file request is within the current directory

  fileName = /root/ + fileName ;

  // Open the requested file

  FileInputStream fis = null ;

  boolean fileExists = true ;

  try {

  fis = new FileInputStream(fileName);

  } catch (FileNotFoundException e) {

  fileExists = false ;

  }

  // Construct the response message

  String statusLine = null;

  String contentTypeLine = null;

  String entityBody = null;

  if (fileExists) {

  statusLine = HTTP/ OK + CRLF;

  contentTypeLine = ContentType: +

  contentType(fileName) + CRLF;

  } else {

  statusLine = HTTP/ Not Found + CRLF;

  contentTypeLine = ContentType: text/html + CRLF;

  entityBody = <HTML> +

  <HEAD><TITLE>Not Found</TITLE></HEAD> +

  <BODY>Not Found</BODY></HTML>;

  }

  // Send the status line

  oswriteBytes(statusLine);

  // Send the content type line

  oswriteBytes(contentTypeLine);

  // Send a blank line to indicate the end of the header lines

  oswriteBytes(CRLF);

  // Send the entity body

  if (fileExists) {

  sendBytes(fis os);

  fisclose();

  } else {

  fileExists = false;

  oswriteBytes(entityBody) ;

  }

  // Close streams and socket

  osclose();

  brclose();

  socketclose();

  }

  private static void sendBytes(FileInputStream fis

  OutputStream os) throws Exception {

  // Construct a K buffer to hold bytes on their way to the socket

  byte[] buffer = new byte[];

  int bytes = ;

  // Copy requested file into the sockets output stream

  while ((bytes = fisread(buffer)) != ) {

  oswrite(buffer bytes);

  }

  }

  private static String contentType(String fileName) {

  if(fileNameendsWith() || fileNameendsWith(l)) {

  return text/html;

  }

  else if (fileNameendsWith(gif)) {

  return image/gif;

  }

  else if (fileNameendsWith(jpg) || fileNameendsWith(jpeg)){

  return image/jpeg;

  }

  return application/octetstream ;

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25715.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.