package WebServer;
import java.net.*;
import java.io.*;
class Process implements Runnable{
Socket s;
public Process(Socket s1){
s=s1;
}
public void run() {
try{
PrintStream out=new PrintStream(s.getOutputStream());
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
String info=in.readLine();
System.out.println("now got "+info);
out.println("HTTP/1.0 200 OK");
out.println("MIME_version:1.0");
out.println("Content_Type:text/html");
/*
* 浏览器请求形如 GET /T/1.HTML HTTP/1.1
* sp1,sp2为第一次和第二次出现空格的位置
* filename为从浏览器中提出文件路径和名称 如 T/1.HTML
*/
int sp1=info.indexOf(' ');
int sp2=info.indexOf(' ',sp1+1);
String fileName=info.substring(sp1+2,sp2);
if(fileName.equals("")||fileName.endsWith("/")){
fileName+="index.html";
}else if((sp1=fileName.indexOf(".htm"))!=-1){
fileName=fileName.substring(0,sp1)+".html";
}
System.out.println("Sending "+fileName);
File fi=new File(fileName);
//判断输入的文件是否存在,不存在返回错误。
if(!fi.exists()){
fileName="error.html";
}
fi=new File(fileName);
System.out.println("Sending "+fileName);
InputStream fs=new FileInputStream(fi);
int n=fs.available(); //n为文件长度
byte buf[]=new byte[1024];
out.println("Content_Length: "+n);
out.println("");
while((n=fs.read(buf))>=0){
out.write(buf,0,n);
}
out.close();
s.close();
in.close();
}
catch(IOException e){
System.out.println("Exception: "+e);
}
}
}
public class MyWebServer {
/**
* @mc1035
*/
public static void main(String[] args) {
try{
ServerSocket ss=new ServerSocket(80);
System.out.println("Web Server OK");
while(true){
Socket s=ss.accept();
Process p=new Process(s);
Thread t=new Thread(p);
t.start();
}
}catch(Exception e){
System.out.println(e);
}
}
}
本段程序主要实现了静态web服务,功能是监听80端口,从IE浏览器端获得输入的网页文件,格式为:http://127.0.0.1/test.html或http://127.0.0.1/
其目录结构为:
webserver:/
test.html
error.html
index.html
WebServer/
MyWebServer.class
Process.class
MyWebServer.java
其中有/的为目录。
![]()
![]()
![]()
import java.net.*;
import java.io.*;
class Process implements Runnable{
Socket s;
public Process(Socket s1){
s=s1;
}
public void run() {
try{
PrintStream out=new PrintStream(s.getOutputStream());
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
String info=in.readLine();
System.out.println("now got "+info);
out.println("HTTP/1.0 200 OK");
out.println("MIME_version:1.0");
out.println("Content_Type:text/html");
/*
* 浏览器请求形如 GET /T/1.HTML HTTP/1.1
* sp1,sp2为第一次和第二次出现空格的位置
* filename为从浏览器中提出文件路径和名称 如 T/1.HTML
*/
int sp1=info.indexOf(' ');
int sp2=info.indexOf(' ',sp1+1);
String fileName=info.substring(sp1+2,sp2);
if(fileName.equals("")||fileName.endsWith("/")){
fileName+="index.html";
}else if((sp1=fileName.indexOf(".htm"))!=-1){
fileName=fileName.substring(0,sp1)+".html";
}
System.out.println("Sending "+fileName);
File fi=new File(fileName);
//判断输入的文件是否存在,不存在返回错误。
if(!fi.exists()){
fileName="error.html";
}
fi=new File(fileName);
System.out.println("Sending "+fileName);
InputStream fs=new FileInputStream(fi);
int n=fs.available(); //n为文件长度
byte buf[]=new byte[1024];
out.println("Content_Length: "+n);
out.println("");
while((n=fs.read(buf))>=0){
out.write(buf,0,n);
}
out.close();
s.close();
in.close();
}
catch(IOException e){
System.out.println("Exception: "+e);
}
}
}
public class MyWebServer {
/**
* @mc1035
*/
public static void main(String[] args) {
try{
ServerSocket ss=new ServerSocket(80);
System.out.println("Web Server OK");
while(true){
Socket s=ss.accept();
Process p=new Process(s);
Thread t=new Thread(p);
t.start();
}
}catch(Exception e){
System.out.println(e);
}
}
}
本段程序主要实现了静态web服务,功能是监听80端口,从IE浏览器端获得输入的网页文件,格式为:http://127.0.0.1/test.html或http://127.0.0.1/
其目录结构为:
webserver:/
test.html
error.html
index.html
WebServer/
MyWebServer.class
Process.class
MyWebServer.java
其中有/的为目录。