服务器

位置:IT落伍者 >> 服务器 >> 浏览文章

Java实现简单web服务器


发布日期:2022年02月26日
 
Java实现简单web服务器

该程序简单实现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 ;

}

}

               

上一篇:用Socket类实现HTTP协议客户端应用

下一篇:Java Socket应答与HTTP服务器的瓜葛