`
cloudtech
  • 浏览: 4578512 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

java网络编程之HTTP服务器的实现

 
阅读更多
java网络编程之HTTP服务器的实现
发布自己的JAVA版本.因源代码太大,现分开发布:
import java.io.*;
import java.awt.*;
import java.util.*;
import java.net.*;
import java.awt.event.*;
import java.lang.*;
import java.sql.*;
import java.util.Date;
import java.sql.ResultSet.*;
class Self{//日期mydatetime类
String year;
String month;
String day;
String datetime;
Date date;
String week;
Self()
{
date=new Date();
year=this.getyear();
month=this.getmonth();
day=this.getday();
datetime=this.getdatetime();
week=this.getWeek();
}
String getyear()
{
String temp;
int index;
index=this.date.toString().lastIndexOf(" ");
temp=this.date.toString().substring(index,date.toString().length());
return temp;
}
String getmonth()
{
String temp;
temp=this.date.toString().substring(4,7);
if(temp.equals("Feb"))
return "2";
else if(temp.equals("Mar"))
return "3";
else if(temp.equals("Apr"))
return "4";
else if(temp.equals("May"))
return "5";
else if(temp.equals("Jun"))
return "6";
else if(temp.equals("Jul"))
return "7";
else if(temp.equals("Aug"))
return "8";
else if(temp.equals("Sep"))
return "9";
else if(temp.equals("Oct"))
return "10";
else if(temp.equals("Nov"))
return "11";
else if(temp.equals("Dec"))
return "12";
else
return "1";

}
String getday()
{
String temp;
temp=date.toString().substring(8,10);
return temp;

}
String getdatetime()
{
String temp;
temp=date.toString().substring(11,19);
return temp;
}
String getWeek()
{
String temp=this.date.toString().substring(0,3);
if(temp.equals("Mon"))
{
return "星期一";
}
else if(temp.equals("Tue"))
{
return "星期二";
}
else if(temp.equals("Wed"))
{
return "星期三";
}
else if(temp.equals("Thu"))
{
return "星期四";
}
else if(temp.equals("Fri"))
{
return "星期五";
}
else if(temp.equals("Sat"))
{
return "星期六";
}
else if(temp.equals("Sun"))
{
return "星期七";
}
else
return "";
}
String Show()
{
return this.getyear()+"-"+this.getmonth()+"-"+this.getday()+" "+this.getdatetime();
}
}
/*class File_Name_Directory extends Frame implements ActionListener {
FileDialog File_Dialog;//根文件设置
Button Confirm;
Button Cancle;
Button Set_File;
TextField Directory_Name_Text;
TextField File_Name_Text;
Label Directory_Label;
Label File_Label;
String Root_File;
String Root_Directoy;
File_Name_Directory()
{
this.setTitle("根目录设置");
File_Dialog=new FileDialog(this,"根目录设置",FileDialog.LOAD);
Confirm=new Button("确定");
Set_File=new Button("设置");
Cancle=new Button("取消");
Directory_Label=new Label("根目录");
File_Label=new Label("默认Index文件");
File_Name_Text=new TextField();
Directory_Name_Text=new TextField();
this.setLayout(new GridLayout());
this.add(Directory_Label);
this.add(Directory_Name_Text);
this.add(File_Label);
this.add(File_Name_Text);
this.add(Set_File);
this.add(Confirm);
this.add(Cancle);
this.setResizable(false);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent windowevent)
{
setVisible(false);
}
});
Confirm.addActionListener(this);
Set_File.addActionListener(this);
Cancle.addActionListener(this);
this.pack();
this.setVisible(true);
}
String CheckInformation(String temp)
{
return temp;
}
public void actionPerformed(ActionEvent ee)
{
if(ee.getSource()==Confirm)
{
if(this.Directory_Name_Text.getText().length()!=0)
{
if(this.File_Name_Text.getText().length()!=0)
{
this.Root_File=this.File_Name_Text.getText();
System.out.println("current "+this.Root_File+"File");
this.Root_Directoy=this.Directory_Name_Text.getText();
System.out.println(this.Root_Directoy+"Directory");
}
else
{}
//this.setVisible(false);
}
else
{}
this.setVisible(false);
}
else if(ee.getSource()==Set_File)
{
this.File_Dialog.setVisible(true);
this.Directory_Name_Text.setText(this.File_Dialog.getDirectory());
this.File_Name_Text.setText(this.File_Dialog.getFile());
}
else if(ee.getSource()==Cancle)
{
this.File_Name_Text.setText("");
this.Directory_Name_Text.setText("");
}
}
}*/
class Help extends Frame implements ActionListener{
Button b;
TextArea information;
Help()
{
String author;
author="Author :Liubing"+"/n"+"Address:Jinan Univirsity School of Information and Technoloy "+"/n"+"TEL:(0531)6304501";
author=author+"/nQQ:286526084";
author=author+"/nVersion Information:Version 1.0 HTTP Common Server";
author=author+"/nUser Object :Web-Lover,Personnel-WebLover,Programmer of Web";
author=author+"/n Welcome to use ";
this.setLayout(new BorderLayout());
information=new TextArea(author);
information.setFont(new Font("Courier",Font.BOLD,12));
b=new Button("Exit");
this.setTitle("HTTP Author");
this.setBackground(Color.BLUE);
this.setSize(100,100);
this.setResizable(false);
this.add(information,BorderLayout.CENTER);
this.add(b,BorderLayout.NORTH);
this.information.setEnabled(false);
this.pack();
this.setVisible(true);
b.addActionListener(this);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent windowEvent)
{
setVisible(false);
}
});
}
public void actionPerformed(ActionEvent ac)
{
if(ac.getSource()==b)
{
this.setVisible(false);
}
}
}
class Infor implements Runnable{
Socket cSocket;
File file;
//Search_Add_Records search;
//String Directory;
public Infor(Socket cSocket)
{
try{
this.cSocket=cSocket;
// search.Get_Record();
//Directory=this.search.RootDirectory.replaceAll("//","/");
//System.out.println(Directory+"RootDirectory");
}catch(Exception ecod)
{}
}
public void run()
{
try{
String Ip="localhost";
String AInfor;
String ADate;
String DDate;
int Port;
String File_Name;
// File file;
PrintStream response=new PrintStream(this.cSocket.getOutputStream());
BufferedReader request=new BufferedReader(new InputStreamReader(this.cSocket.getInputStream()));
String receive=request.readLine();
AInfor=receive;
Ip=this.cSocket.getInetAddress().getHostAddress();
ADate=new Self().Show().toString();
Port=this.cSocket.getPort();
int sp1=receive.indexOf(" ");
int sp2=receive.indexOf(" ",sp1+1);
File_Name=receive.substring(sp1+2,sp2);
if(File_Name.equals("")||File_Name.endsWith("/"))
{
//if(!this.search.RootFile.equals(""))
// File_Name=this.search.RootFile;//"index.htm";
//else
File_Name="index.htm";
}
// if(this.Directory.equals(""))
// file=new File("F:/li/倪/"+File_Name);//(+File_Name);
//else
//file=new File(this.Directory,File_Name);
file=new File("WWWRoot/"+File_Name);
System.out.println("Get :"+receive.toString());
if(file.exists()==true)
{
InputStream fs=new FileInputStream(file);
int number=fs.available();
byte []buf=new byte[1024];
while((number=fs.read(buf))>0)
{
response.write(buf,0,number);
}
DDate=new Self().Show().toString();
System.out.println(Ip+":"+AInfor);
System.out.println(ADate+":"+DDate+":"+Port);
fs.close();
response.close();
request.close();
this.cSocket.close();
}
else
{
String c="<html><title>刘冰提示你:</title><body>你无法打开此网页<p>请你与管理员联系</body></html>";
response.println(c);
}
}catch(Exception my_e)
{
System.out.println(my_e.toString());
}
}

}
class Server {
HttpServer Http_Server;
ServerSocket Server_Socket;
TextArea sStatus;
int Port;
public Server(HttpServer Http_Server)
{
try{
this.Port=80;
Server_Socket=new ServerSocket(this.Port);
this.Http_Server=Http_Server;
this.sStatus=this.Http_Server.Information;
this.sStatus.append("Http Server is ruunning....");
this.sStatus.append("/nThr current time is :"+new Self().Show());
}catch(Exception exception)
{
this.sStatus.append("Server can't running....");
}
while(true)
{
try{
Socket cSocket=Server_Socket.accept();
Infor Infor_Porcess=new Infor(cSocket);
Thread Tr=new Thread(Infor_Porcess);
Tr.start();
}catch(Exception excep)
{
this.sStatus.append("/nServer is in error....");
}
}
}
}

public class HttpServer extends Frame implements ActionListener{
MenuBar Mbar;
Menu Work_Status,Dirctory_Manager,Access_Manager,Help_System;
MenuItem Work_Status_Run,Work_Status_Stop,Work_Status_Exit;
MenuItem Dirctory_Manager_Root,Dirctory_Manager_Defaur;
MenuItem Access_Manager_View,Access_Manager_Delete;
MenuItem Help_System_Author,Help_System_Content;
TextArea Information;
int ScreenWidth,ScreenHeight;
Server Server_Thread;
boolean flag=false;//测试RUN,STOP;
FileDialog filedialog;
HttpServer()
{
Mbar=new MenuBar();
Work_Status=new Menu("Work Status");
Work_Status_Run=new MenuItem("Run");
Work_Status_Stop=new MenuItem("Stop");
Work_Status_Exit=new MenuItem("Exit");
Dirctory_Manager=new Menu("Dirctory Manager");
Dirctory_Manager_Root=new MenuItem("Set Root Directory");
Dirctory_Manager_Defaur=new MenuItem("Defauer Dirctory ");
Access_Manager=new Menu("Access Manager");
Access_Manager_View=new MenuItem("View Mangaer");
Access_Manager_Delete=new MenuItem("Delete Access");
Help_System=new Menu("Help System");
Help_System_Author=new MenuItem("About Author");
Help_System_Content=new MenuItem("Context");
FileDialog filedialog=new FileDialog(this,"根目录设置",FileDialog.LOAD);
Mbar.add(Work_Status);
Mbar.add(Dirctory_Manager);
Mbar.add(Access_Manager);
Mbar.add(Help_System);
Work_Status.add(Work_Status_Run);
Work_Status.add(Work_Status_Stop);
Work_Status.add(Work_Status_Exit);
Dirctory_Manager.add(Dirctory_Manager_Root);
Dirctory_Manager.add(Dirctory_Manager_Defaur);
Access_Manager.add(Access_Manager_View);
Access_Manager.add(Access_Manager_Delete);
Help_System.add(Help_System_Author);
Help_System.add(Help_System_Content);
this.setTitle("HTTP-Server");
Information=new TextArea();
ScreenWidth=getToolkit().getScreenSize().width;
ScreenHeight=getToolkit().getScreenSize().height;
setSize(ScreenWidth/2,ScreenHeight/2);
setLocation((ScreenWidth-getWidth())/2,(ScreenHeight-getHeight())/2);
this.setMenuBar(Mbar);
this.add(Information);
this.setResizable(false);
this.setVisible(true);
this.pack();
this.Information.setEnabled(false);
Work_Status_Run.addActionListener(this);
Work_Status_Stop.addActionListener(this);
Work_Status_Exit.addActionListener(this);
Dirctory_Manager_Root.addActionListener(this);
Dirctory_Manager_Defaur.addActionListener(this);
Access_Manager_View.addActionListener(this);
Access_Manager_Delete.addActionListener(this);
Help_System_Author.addActionListener(this);
Help_System_Content.addActionListener(this);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent windowEvent)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{

if(e.getSource()==Help_System_Author)
{
Help LiuBing=new Help();
}
else if(e.getSource()==Help_System_Content)
{}
else if(e.getSource()==Dirctory_Manager_Root)
{
//File_Name_Directory filedialog=new File_Name_Directory();
/* try{
filedialog.setVisible(true);
String temp_Directory=filedialog.getDirectory();
String temp_File=filedialog.getFile();
System.out.println("Current Root is "+temp_Directory+temp_File);
Search_Add_Records SearchAdd=new Search_Add_Records();
if(temp_Directory.equals(""))
{
temp_Directory="F:/javaprocer/newproject/Ver1.0通用HTTP服务器/WWWRoot/倪/";
}
if(temp_File.equals(""))
{
temp_File="index.htm";
}
SearchAdd.Insert_Record(temp_Directory,temp_File);
}catch(Exception eses)
{}*/
}
else if(e.getSource()==Dirctory_Manager_Defaur)
{

}
else if(e.getSource()==Access_Manager_View)
{
try{
// AccessView Access_View=new AccessView();
}catch(Exception excep)
{}

}
else if(e.getSource()==Access_Manager_Delete)
{}
else if(e.getSource()==Work_Status_Run)
{
try{
/*if(flag)
{
this.Server_Thread.resume();
this.Information.append("/n服务器已启动");
}
else
{ */
//this.Server_Thread=new ServerThread(this);
//this.Server_Thread.start();
// }

}catch(Exception excep)
{
this.Information.append("运行出现错误");
}
}
else if(e.getSource()==Work_Status_Stop)
{
try{
// this.Server_Thread.suspend();
//this.Server_Thread.wait();
// this.Information.append("/n服务器已停止");
// flag=true;
}catch(Exception S_e)
{}
}
else if(e.getSource()==Work_Status_Exit)
{
System.exit(0);
}
}
public static void main(String args[])
{
try{
HttpServer http=new HttpServer();
http.Server_Thread=new Server(http);
}catch(Exception exception)
{
System.out.println("Server Can't running.......");
}
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics