转载:http://hi.baidu.com/triceratops/blog
import java.io.*;
import java.net.*;
public class HttpServer{
private ServerSocket server;
public void startService(int port) throws IOException{
System.out.println("service started...");
server = new ServerSocket(port);
while (true)
new ServiceThread(server.accept()).start();
}
public static void main(String[] args) throws Exception{
if(args.length!=1){
System.out.println("Usage: java HttpServer [port]");
System.exit(1);
}
new HttpServer().startService(Integer.parseInt(args[0]));
}
}
public class ServiceThread extends Thread{
private Socket serverEndPoint;
private InputStream in;
private OutputStream out;
private byte[] fileBytes;
private String threadId;
private static int id = 0;
ServiceThread(Socket serverEndPoint){
this.serverEndPoint = serverEndPoint;
threadId = "[No."+id+++"]";
}
private void parseHttpRequest() throws IOException{
System.out.println(this+"=========================");
in = serverEndPoint.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String header="";
String fileName;
if(br.ready())
header = br.readLine();
System.out.println(this+header);
fileName = header.split(" ")[1];
if(fileName.startsWith("/"))
fileName = fileName.substring(1);
if(fileName.indexOf("?")!=-1)
fileName = fileName.substring(0,fileName.indexOf("?"));
if(fileName.length() == 0)
fileName = "index.html";
fileName = fileName.replaceAll("%20"," ");
System.out.println(this+"=========================");
serverEndPoint.shutdownInput();
buildHttpResponse(getRequestedFile(fileName));
}
private boolean getRequestedFile(String fileName) throws IOException{
File file = new File(fileName);
if(!file.exists()||file.isDirectory())
return false;
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int i;
while((i=fis.read(buf))!=-1)
baos.write(buf,0,i);
fileBytes = baos.toByteArray();
return true;
}
private void buildHttpResponse(boolean fileExist) throws IOException{
out = serverEndPoint.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
if(fileExist){
dos.writeBytes("HTTP/1.0 200 Document Follows\r\n");
dos.writeBytes("\r\n");
dos.write(fileBytes);
}
else{
dos.writeBytes("HTTP/1.0 404 Document Follows\r\n");
dos.writeBytes("\r\n");
}
serverEndPoint.shutdownOutput();
}
public void run(){
try{
parseHttpRequest();
}
catch(Exception e){
System.out.println(this);
e.printStackTrace();
}
}
public String toString(){
return threadId;
}
}
分享到:
相关推荐
webserver implemented in C all you have to do is modify the port to what you wish to be, and perfectly works. I included Makefile for you(for unix/linux users).
"All Algorithms implemented in Java.zip"这个压缩包文件,正如其名,包含了用Java实现的各种算法,这对于学习和理解算法,提升编程技能具有极大的价值。接下来,我们将深入探讨其中可能涵盖的一些重要算法及其在...
"All Algorithms implemented in Java(3).zip" 提供的是一份包含多种算法实现的Java代码库,对于学习和理解算法有着极高的价值。这个压缩包中的"Java-master"文件夹很可能包含了不同分类的算法实现,如排序、搜索、...
"All Algorithms implemented in Java"这个压缩包文件很可能包含了一系列经典的算法实现,这些实现可以帮助开发者深入理解算法并提升编程技能。以下是根据标题和描述可能涵盖的一些重要知识点: 1. **排序算法**: ...
在"All Algorithms implemented in Java(4).zip"这个压缩包中,包含了一个名为"Java-master"的项目,它集成了众多经典算法的Java实现。下面我们将深入探讨这些算法的核心原理及其在实际应用中的价值。 1. **排序...
本资料集“所有算法实现 in Java(2).zip”中包含了一个名为“Java-master”的项目,该项目深入浅出地展示了Java语言在实现各种算法时的应用。下面我们将详细探讨这个压缩包中的知识内容。 一、排序算法 排序是数据...
Java编程语言以其跨平台、面向对象的特性在IT行业中占据着重要的地位,广泛应用于软件开发、Web应用、移动应用等领域。这个压缩包“所有算法用Java实现(1).zip”显然是一个包含Java实现的各种算法的资源集合。让我们...
simply implemented the http server by java
简单HTTP服务器 Java中的简单HTTP服务器 :globe_with_meridians: :open_...克隆项目: git clone https://github.com/MalakSadek/Simple-HTTP-Server 将CD放入目录并编译文件,然后运行源代码: cd Java-Web-Server-
标题中的“Enhanced Modbus library implemented in the Java programming”指的是一个增强版的Modbus库,该库是用Java编程语言实现的。Modbus是一种广泛使用的通信协议,主要用于工业自动化设备之间的数据交换,如...
A two layer perceptron implemented in MatLab to recognize handwritten digits based on the MNIST dataset.
Method: In this paper, we carried out an empirical study to explore the logging practice in open source software projects so as to establish a basic understanding on how logging practice is applied ...
A two layer perceptron implemented in MatLab to recognize handwritten digits based on the MNIST dataset..zip
multi-layer perceptron implemented in java.multi-layer perceptron implemented in java.multi-layer perceptron implemented in java.
A simple, illustrative implementation of a single-layered perceptron in Java. When a pattern is impressed on the perceptron the activation of the network is adjusted according to an activation ...
PRML algorithms implemented in Python
You’ll explore the important classes in the Java collections framework (JCF), how they’re are implemented, and how they are expected to perform. By emphasizing practical knowledge and skills over ...
Socket.IO server implemented on Java. Realtime java framework
-JADE (Java Agent DEvelopment Framework) is a software framework fully implemented in Java language.It simplifies the implementation of multi-agent systems through a middle-ware that claims to comply ...
入门指南 在您捐款之前,请阅读我们的捐款指南。 社区频道 我们在Discord和Gitter!社区频道是您提问和获得帮助的好方法。请加入我们! ...请参阅我们的目录,以获得更简单的导航和更好的项目概述。