跨平台通信研究之二服务器与浏览器客户端通信
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;
//支持多用户访问
//http解析
//文件传输
public class tomcat {
private ServerSocket server;
//private HashMap map;
// Socket socket;
public static final String webroot = "d:/";
// public tomcat(){
//
// }
// 启动服务器
public void start() throws IOException {
server = new ServerSocket(100);
while (true) {
Socket socket = server.accept();
Service service = new Service(socket);
new Thread(service).start();
}
}
class Service implements Runnable {
private Socket socket;
private HashMap paramentermap=null;
private HashMap map =null;
public HashMap getParamentermap() {
return paramentermap;
}
public void setParamentermap(HashMap paramentermap) {
this.paramentermap = paramentermap;
}
public HashMap getMap() {
return map;
}
public void setMap(HashMap map) {
this.map = map;
}
public Service(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("开始提供一个用户提供服务");
try {
protocolHandle();
// response();
parameterHandle();
execute();
socket.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* try { OutputStream out=socket.getOutputStream(); InputStream
* in=socket.getInputStream(); } catch (IOException e) { // TODO
* Auto-generated catch block e.printStackTrace(); }
*/
}
private void protocolHandle() throws IOException {
map=new HashMap();
InputStream in = socket.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
String line = reader.readLine();
if (line == null) {
return;
}
String arr[] = line.split(" ");
map.put("method", arr[0]);
System.out.println(arr[0]);
System.out.println(arr[1]);
if (arr[0].toLowerCase().equals("get")) {
System.out.println(arr[1]);
String arr2[] = arr[1].split("\\?", 2);
System.out.println(arr2[0]);
System.out.println(arr2.length);
// map.put("resource", arr2[0]);
if (arr2.length >1) {
//System.out.println(arr2[1]);
//System.out.println(arr2[0]);
map.put("parameter", arr2[1]);
map.put("resource", arr2[0]);
} else {
map.put("resource", arr[1]);
}
}
// map.put("resource", arr[1]);
// System.out.println(arr[0]+arr[1]);
while (true) {
line = reader.readLine();
if (line == null || line.equals("")) {
break;
}
arr = line.split(":", 2);
map.put(arr[0], arr[1].trim());
System.out.println(arr[0] + arr[1]);
}
}
/* private void response() throws Exception {
System.out.println("111");
OutputStream out = socket.getOutputStream();
// File file=new File(webroot+map.get("resource")) ;
File file = new File("d:\\a\\" + map.get("resource"));
String mineType = Helper.getMine(Helper.getExtName(file));
System.out.println("55555555");
System.out.println();
if (file.exists()) {
out.write("HTTP/1.1 200 ok".getBytes());
out.write("\r\n".getBytes());
out.write("Server: 服务器".getBytes());
out.write("\r\n".getBytes());
out.write(("content-length:" + file.length()).getBytes());
out.write("\r\n".getBytes());
out.write(("content-Type: " + mineType).getBytes());
out.write("\r\n".getBytes());
out.write("Accept-language:zh-cn".getBytes());
out.write("\r\n".getBytes());
out.write("\r\n".getBytes());
System.out.println("222");
FileInputStream fileIn = new FileInputStream(file);
byte data[] = new byte[fileIn.available()];
fileIn.read(data);
out.write(data);
out.flush();
// out.close();
System.out.println("333");
}
else {
System.out.println("文件不存在");
}
}
*/
public void parameterHandle() {
//paramentermap=null;
paramentermap=new HashMap();
String method = map.get("method").toString();
String paramenter = (String) map.get("parameter");
if (paramenter != null) {
String arr[] = paramenter.split("&");
for (int i = 0; i < arr.length; i++) {
String strs[] = arr[i].split("=");
System.out.println("key=" + strs[0]);
System.out.println("values=" + strs[1]);
paramentermap.put(strs[0], strs[1]);
}
}
setParamentermap(paramentermap);
}
private void execute() throws Exception{
//Iterator i= paramentermap.keySet().iterator();
// while(i.hasNext()) {
/*String key=i.next().toString();
String */
String username=paramentermap.get("username").toString();
String password=paramentermap.get("password").toString();
if(username.equals("admin")&&password.equals("123456"))
{
OutputStream out=socket.getOutputStream();
myoutputstream a=new myoutputstream(out);
//File file = new File("d:\\a\\" + map.get("resource"));
//String mineType = Helper.getMine(Helper.getExtName(file));
a.setMineType("text/html");
a.flush("登陆成功啊啊啊啊啊");
/* out.write("<html".getBytes());
out.write("<body>".getBytes());
out.write("登陆成功".getBytes());
out.write("</body>".getBytes());
out.write("</html>".getBytes());*/
}
else{
/*OutputStream out=socket.getOutputStream();
out.write("<html".getBytes());
out.write("<body>".getBytes());
out.write("登陆失败".getBytes());
out.write("</body>".getBytes());
out.write("</html>".getBytes());
*/
OutputStream out=socket.getOutputStream();
myoutputstream a=new myoutputstream(out);
//File file = new File("d:\\a\\" + map.get("resource"));
//String mineType = Helper.getMine(Helper.getExtName(file));
a.setMineType("text/html");
a.flush("登陆失败");
}
}
}
public static void main(String[] args) throws IOException {
new tomcat().start();
}
}