import java.io.* ;
import java.net.* ;
import java.util.* ;
public final class WebServer {
// Count the number of connection
static int n = 0;
public static void main(String argv[]) throws Exception
{
// Set the port number.
int port = 2046;
// Establish the listen socket.
ServerSocket serverSocket = new ServerSocket(port);
// Process HTTP service requests in an infinite loop.
while (true){
// Listen for a TCP connection request.
Socket socket = serverSocket.accept();
n++;
HttpRequest request = new HttpRequest(socket,n);
Thread thread = new Thread(request);
thread.start();
}
}
}
final class HttpRequest implements Runnable{
final static String CRLF = "\r\n";
Socket socket;
int n;
// Constructor
public HttpRequest(Socket socket, int n) throws Exception
{
this.socket = socket;
this.n = n;
}
// Implement the run() method of the Runnable interface.
public void run()
{
try {
processRequest();
} catch (Exception e) {
System.out.println(e);
}
}
private void processRequest() throws Exception
{
// Get a reference to the socket's input and output streams.
InputStream is = this.socket.getInputStream();
DataOutputStream os = new DataOutputStream(this.socket.getOutputStream());
// Set up input stream filters.
BufferedReader br = new BufferedReader(new InputStreamReader(is));
// Get the request line of the HTTP request message.
String requestLine = br.readLine();
// Display the request line.
System.out.println();
System.out.println(requestLine);
// Get and display the header lines.
String headerLine = null;
while ((headerLine = br.readLine()).length() != 0) {
System.out.println(headerLine);
}
// Extract the filename from the request line
StringTokenizer tokens = new StringTokenizer(requestLine);
tokens.nextToken(); // skip over the method, which should be "GET"
String fileName = tokens.nextToken();
// Prepend a "." so that file request is within the current directory.
if(fileName.equals("/")){
fileName = "./index.html";
}else{
fileName = "." + 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/1.0 200 OK" + CRLF;
contentTypeLine = "Content-type: " + contentType(fileName) + CRLF;
} else {
statusLine = "HTTP/1.0 404 Not Found" + CRLF;
contentTypeLine = "No contents" + CRLF;
entityBody = "<html>" +"<head><title>Not Found</title></head>" +
"<body>Not Found</body></html>";
}
// Send the status line.
os.writeBytes(statusLine);
// Send the content type line.
os.writeBytes(contentTypeLine);
// Send a blank line to indicate the end of the header lines.
os.writeBytes(CRLF);
System.out.println("The number of connection: "+ n);
System.out.println(statusLine);
System.out.println(contentTypeLine);
// Send the entity body.
if (fileExists) {
sendBytes(fis, os);
fis.close();
} else {
os.writeBytes(entityBody);
}
os.close();
is.close();
br.close();
socket.close();
}
private static void sendBytes(FileInputStream fis, OutputStream os) throws Exception {
// Construct a 1K buffer to hold bytes on their way to the socket.
byte[] buffer = new byte[1024];
int bytes = 0;
// Copy requested file into the socket's output stream.
while((bytes = fis.read(buffer)) != -1) {
os.write(buffer, 0, bytes);
}
}
private static String contentType(String fileName) {
if (fileName.endsWith(".htm") || fileName.endsWith(".html")) {
return "text/html";
}
if (fileName.endsWith(".gif")) {
return "image/gif";
}
if (fileName.endsWith(".jpg")) {
return "image/jpeg";
}
if(fileName.endsWith(".java"))
{
return "java file";
}
return "application/octet-stream";
}
}
分享到:
相关推荐
如何在linux服务器上同时搭建php运行环境(apache)和java运行环境(tomcat) 一台linux上同时运行php和java网站项目 一台linux上同时运行apache httpd和java tomcat
今天怀着好奇的心理去搭建了一个JAVA编译环境下的后台服务器。听说用PHP搭建服务器的居多,但是我们做大Android的最熟悉的还是Java了,所以下面我就开始搭建这个服务器。很简单。。。 首先我下载了一个myelipse应为...
本项目开发前提是,在本地windows机器上搭建了MQ,并建立好发送队列管理器,本地队列,远程队列,发送通道;在阿里云服务器上也搭建了MQ,建立了接收队列管理器,接收...同一台机器同一个队列管理器上发送和接收消息
minio 文件服务器环境搭建/以及示例代码,方便搭建文件服务器,代码包含传统的本地保存、minio保存、s3保存等示例代码。
logvewer 介绍==============logviewer 是一个可以方便开发人员通过浏览器查看和下载远程服务器集群日志或指定文件的应用# 解决的问题- 针对服务器比较少的情况下,运维人员无需搭建日志推送或处理程序,使用该应用...
客户的环境是集群环境,所以文件上传到服务器是不行的,搭建文件服务器也不在考虑之中,为此单独搭建了一个ftp供客户存储文件。主要功能是使用前台plupload插件实现批量文件上传功能,后台把文件流传到FTP上面以及...
搭建Javaweb个人网站:从阿里或者腾讯的云服务器上购置服务器(下面的是阿里云ECS服务器),购买完后创建一个实例(系 统选CentOS)。 有的rpm包是在红帽子下编译的,有的是在其他平台下编译的,含有noarch的包说明...
linux上搭建mysql两台mysql服务器,一台mycat。使用java代码实-20190920-mycat
使用Java编写的应用程序,既可以在一台单独的电脑上运行,也可以被分布在一个网络的服务器端和客户端运行。另外,Java还可以被用来编写容量很小的应用程序模块或者applet,做为网页的一部分使用。applet可使网页使用...
网络与通信:数据传输、信号处理、网络协议、网络与通信硬件、网络安全网络与通信是一个非常广泛的领域,它涉及到计算机科学、电子工程、数学等多个学科的知识。 云计算与大数据:包括云计算平台、大数据分析、人工...
JetBrains 每年都会发布一个开发者生态系统调查报告,根据该报告的统计数据,最近几年全球约有 1/3 的专业程序员将 Java 作为主要编程语言,这一比例在我国更是高达 1/2,我国也因此成为全球 Java 程序员最多的国家...
网络与通信:数据传输、信号处理、网络协议、网络与通信硬件、网络安全网络与通信是一个非常广泛的领域,它涉及到计算机科学、电子工程、数学等多个学科的知识。 云计算与大数据:数据集、包括云计算平台、大数据...
Java Bean 是可复用的组件,对Java Bean并没有严格的规范,理论上讲,任何一个Java类都可以是一个Bean。但通常情况下,由于Java Bean是被容器所创建(如Tomcat)的,所以Java Bean应具有一个无参的构造器,另外,...
实例127 一个文件变成多个小文件 178 实例128 多个小文件合成一个文件 181 实例129 统计指定文件中的字符个数 183 实例130 对象的序列化与反序列化 185 实例131 同时显示多个文件 187 实例132...
实例128 多个小文件合成一个文件 181 实例129 统计指定文件中的字符个数 183 实例130 对象的序列化与反序列化 185 实例131 同时显示多个文件 187 实例132 生成zip压缩文件 189 实例133 解压缩zip文件 192 实例134 ...
实例128 多个小文件合成一个文件 181 实例129 统计指定文件中的字符个数 183 实例130 对象的序列化与反序列化 185 实例131 同时显示多个文件 187 实例132 生成zip压缩文件 189 实例133 解压缩zip...
Docker 本身就是基于 Linux 的,所以首先以我的一台服务器做实验。虽然最后跑 wordcount 已经由于内存不足而崩掉,但是之前的过程还是可以参考的。 连接服务器 使用 ssh 命令连接远程服务器。 ssh root@[Your IP ...
文档详细描述,linux不通外网的情况下,通过在另一台通外网的服务器搭建socks5代理服务,不通外网的服务器通过socks5代理,成功向外网发送邮件的功能。
162.集群中有 3 台服务器,其中一个节点宕机,这个时候 zookeeper 还可以使用吗? 163.说一下 zookeeper 的通知机制? 十七、MySql 164.数据库的三范式是什么? 165.一张自增表里面总共有 7 条数据,删除了最后 2 条...