用Java实现网站服务_器
时间:2007-10-27 来源:不详 作者:林仙
StringdestIP=client.getInetAddress().toString();//客户机IP地址
intdestport=client.getPort();//客户机端口号
System.out.println("Connection" counter ":connectedto" destIP "onport" destport ".");
PrintStreamoutstream=newPrintStream(client.getOutputStream());
DataInputStreaminstream=newDataInputStream(client.getInputStream());
Stringinline=instream.readLine();//读取Web浏览器提交的请求信息
System.out.println("Received:" inline);
if(getrequest(inline)){//如果是GET请求
Stringfilename=getfilename(inline);
Filefile=newFile(filename);
if(file.exists()){//若文件存在,则将文件送给Web浏览器
System.out.println(filename "requested.");
outstream.println("HTTP/1.0200OK");
outstream.println("MIME_version:1.0");
outstream.println("Content_Type:text/html");
intlen=(int)file.length(); dedecms.com
outstream.println("Content_Length:" len);
outstream.println("");
sendfile(outstream,file);//发送文件
outstream.flush();
}else{//文件不存在时
Stringnotfound="<html><head><title>NotFound</title></head>
<body><h1>Error404-filenotfound</h1></body></html>";
outstream.println("HTTP/1.0404nofound");
outstream.println("Content_Type:text/html");
outstream.println("Content_Length:" notfound.length() 2);
outstream.println("");
outstream.println(notfound);
outstream.flush();
}
}
longm1=1;
while(m1<11100000){m1 ;}//延时
client.close();
}catch(IOExceptione){
System.out.println("Exception:" e);
上一篇:FreeBSD平台下网站服务_器架设攻略 下一篇:Win2003架设网站服务_器
文章评论
共有位Admini5网友发表了评论 查看完整内容