|
httpserver原理:服务器端 打开一个socket,一直处在监听tomcat指定的 ip 的指定端口,
一旦有访问的,就开启一个线程去处理,请参看案例。
httpserver原理:服务器端 打开一个socket,一直处在监听tomcat指定的 ip 的指定端口,一旦有访问的,就开启一个线程去处理,代码如下:
-------------------------------server:
package com.kaobian;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import com.session.HttpSession;
public class HttpServer {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(8888);
while (true) {
Socket socket = server.accept();
HttpSession session = new HttpSession(socket);
new Thread(session).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
---------------------------------处理线程
package com.session;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import com.config.Config;
public class HttpSession implements Runnable {
private String path = Config.getConfig().getPath();
private Socket socket ;
public HttpSession(Socket socket){
super();
this.socket = socket;
}
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
OutputStream out = this.socket.getOutputStream();
String command = null;
while((command = br.readLine()) != null){
System.out.println("浏览器的指令:"+command);
if(command.length() <3){
break;
}
String result = command.substring(0,3);
if(result.equalsIgnoreCase("GET")){
int begin = command.indexOf("/")+1;
int end = command.lastIndexOf(" ");
String fileName = command.substring(begin,end);
doGet(fileName,out);
}
}
out.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void doGet(String fileName, OutputStream out) {
File file = new File(path+fileName);
if(!file.isDirectory()){
if(!file.exists()){
file = new File(path + Config.getConfig().getDefaultPage());
}
}else {
file = new File(path + Config.getConfig().getDefaultPage());
}
InputStream is = null;
try {
is = new FileInputStream(file);
byte[] data =new byte[1024];
int len = 0;
while((len = is.read(data)) != -1){
out.write(data,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
is = null;
}
}
}
}
}
---------------------------读取配置文件
package com.config;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Config {
private String path ;
private String defaultPage;
private static Config config ;
public static Config getConfig(){
if(config == null){
config = new Config();
config.init();
}
return config;
}
private void init(){
Properties properties = new Properties();
try {
InputStream reader = new FileInputStream(new File("config.properties"));
properties.load(reader);
this.path = properties.getProperty("path");
this.defaultPage = properties.getProperty("defaultIndex");
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private Config(){
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getDefaultPage() {
return defaultPage;
}
public void setDefaultPage(String defaultPage) {
this.defaultPage = defaultPage;
}
}
------------------配置文件要放到工程的根目录下,要不然自行修改属性文件的位置
path=c://
defaultIndex=div.html
文章出处:http://kaobian.iteye.com/blog/1126616 |
|
|