⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 70bd49973f57001b1647dc3ac84ad89f

📁 《ajax编程技术与实例》的所有的案例
💻
字号:
package com.bam;import java.awt.Dimension;import java.awt.Rectangle;import java.awt.Robot;import java.awt.Toolkit;import java.awt.image.BufferedImage;import java.io.BufferedInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.IOException;import java.io.OutputStream;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import java.net.SocketException;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.Random;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.imageio.ImageIO;public class AJAXServer implements Runnable {	ServerSocket serverSocket;	Socket conn;	Thread clientConnection;	boolean log = AJAX_Desktop.log;	boolean logdebug = AJAX_Desktop.logdebug;	boolean loginfo = AJAX_Desktop.loginfo;		static int connections = 0;	static final Integer ZERO = new Integer(0);		static HashMap sessions = new HashMap();	private static HashMap Images = ScreenCapture.getImages();	static int chunks = ScreenCapture.getChunks(); 	static int count = 0;		private static int connectedPort;	private int port;	private String Service;	AJAXServer(int port, String Channel) {		this.port = port;		this.Service = Channel;		this.start();	}		/*	 * 启动服务	 * 	 * */	public void start() {		if (clientConnection == null) {			try {				// 在port端口监听连接请求				serverSocket = new ServerSocket(port);				if (loginfo) System.out.println("Listening on port: " + port);			} catch (Exception e) {				e.printStackTrace();				System.exit(0);			}			clientConnection = new Thread(this);			clientConnection.start();		}	}	public void stop() {	}	public void run() {		while (clientConnection != null) {			try {				conn = serverSocket.accept();				conn.setKeepAlive(false);				conn.setReuseAddress(true);				conn.setSoLinger(false, 0);				conn.setSoTimeout(100);				new connectionHandler(conn);			} catch (Exception e) {				e.printStackTrace();				System.exit(0);			}		}	}	/**	 * 为每个连接请求开启一个线程	 *	 */	class connectionHandler implements Runnable {		Socket conn;		Thread kicker = null;		BufferedInputStream instream;		String sessionID;		String str;		InetAddress address; // IP地址		connectionHandler(Socket connin) {			conn = connin;			this.address = conn.getInetAddress();						this.start();		}		/**		 * 为每个连接都启动一个新的线程		 */		public void start() {			if (kicker == null) {				kicker = new Thread(this);				if (logdebug) System.out.println("New Connection Occured to: "						+ address.toString());								kicker.start();			}		}		/**		 * 关闭输出流以及连接;		 */		public void stop() {			try {				conn.getOutputStream().close();				conn.close();				if (logdebug) System.out.println("Connection Terminated: "						+ address.toString());			} catch (Exception e) {				e.printStackTrace();			} finally {								kicker = null;			}		}		/**		 * 线程运行		 */		public void run() {			try {								// 得到输入流				instream = new BufferedInputStream(conn.getInputStream());			} catch (Exception e) {				if (logdebug) System.out.println("kicker run: " + e);				System.exit(1);			}			if (logdebug) System.out.println("Request:");			ArrayList requestLines = new ArrayList();			// 获取请求;			do {				// 读取每行的信息				str = this.getline();				if (str != null) {					if (logdebug) System.out.println(" " + str);										requestLines.add(str);				}			} while (str != null);			if (requestLines.size()==0) return;						// 根据请求,构建一个xhtml文件			String ImportantLine = (String) requestLines.get(0);			String mimeType = "text/xml";			byte[] data = null;			Chunk image = null;			boolean doHeader = true;			if (logdebug) System.out.println("Important Line " + ImportantLine);			if ( ImportantLine.startsWith("GET /remotedesktop.html ")) {				// 请求remotedesktop.html页面				connections++;								// 新建HashMap来存放Session信息				HashMap SessionInfo = new HashMap();								sessions.put(new Integer(connections), SessionInfo);								mimeType = "text/html";				// 用来保存HTML中的table				StringBuffer sb = new StringBuffer();				sb.append("<table cellspacing='0' cellpadding='0'>");				for (int y = 0; y < chunks; y++) {					sb.append("<tr>\n");					for (int x = 0; x < chunks; x++) {						sb.append("<td>");						sb.append("<img src='image");						sb.append(chunks * y + x);						sb.append("_");						sb.append(connections);						sb.append("-0.");						sb.append(AJAX_Desktop.extension);						sb.append("' id='i");						sb.append(chunks * y + x);						sb.append("'></td>\n");						SessionInfo.put(Integer.toString(chunks * y + x), ZERO);					}					sb.append("</tr>");				}				sb.append("</table>");							String FileText = new String(FileStore.RequestPage);				FileText = FileText.replaceFirst("<DYNAMICTEXT>", sb.toString());				FileText = FileText.replaceFirst("<TIMEOUT>", Integer.toString(AJAX_Desktop.refresh_milliseconds));				data = FileText.replaceFirst("<SESSION>", Integer.toString(connections)).getBytes();				System.out.println("New Session from: " + this.address.toString());			}			if (ImportantLine.startsWith("GET /getUpdate?Id=")) {				// 更新页面				String SessionId = ImportantLine.substring(18, ImportantLine.indexOf(" HTTP"));								if (logdebug) System.out.println("Update Request Session Id=" + SessionId);				mimeType = "text/plain";				HashMap SessionInfo = (HashMap) sessions.get(new Integer(SessionId));								StringBuffer sb = new StringBuffer();				Iterator p = SessionInfo.keySet().iterator();				if (logdebug) System.out.println("ImageSizes: " + SessionInfo.size());				Integer version = null;				String Key = null;				while (p.hasNext()) {					Key = (String) p.next();					version = (Integer) SessionInfo.get(Key);					image = (Chunk) Images.get(Key);					if (logdebug)System.out.println("compare: " + Key +" " + version + " " + image.getVersion());					if (version.intValue() != image.getVersion()) {						sb.append("document.getElementById('i");						sb.append(Key);						sb.append("').src='image");						sb.append(Key);						sb.append("_");						sb.append(SessionId);						sb.append("-");						sb.append(count++);						sb.append(".");						sb.append(AJAX_Desktop.extension);						sb.append("';");						// sb.append("alert('Yo');");																	}				}				if (logdebug) System.out.println("Returning: " + sb.toString());			    data = sb.toString().getBytes();			}			if (ImportantLine.startsWith("GET /image")) {				// 请求图片				int underscore = ImportantLine.indexOf("_");				String ImageNumber = ImportantLine.substring(10, underscore);				String SessionId = ImportantLine.substring(underscore+1, ImportantLine						.indexOf("-"));				if (logdebug) System.out.println("Image Request: " + ImageNumber + " Session: " + SessionId);				HashMap SessionInfo = (HashMap) sessions.get(new Integer(SessionId));				mimeType = "image/" + AJAX_Desktop.extension;				image = (Chunk) Images.get(ImageNumber);				if (image==null) return;				SessionInfo.put(ImageNumber, new Integer(image.getVersion()));				data = image.getData();				doHeader = false;				if (logdebug) System.out.println("Image Request... size: " + data.length						+ " id: " + ImageNumber);			}			/* 将客户端的请求放入data中,最后写入输出流 */			if (ImportantLine.startsWith("GET /favicon")) {				mimeType = "image/x-icon";				data = FileStore.FavIcon;				doHeader = false;				if (logdebug)  System.out.println("Icon Request...");			}			if (ImportantLine.startsWith("GET /pc.png")) {				mimeType = "image/png";				data = FileStore.PCImage;				doHeader = false;				if (logdebug)  System.out.println("Image Request...");			}						if (ImportantLine.startsWith("GET /flame.png")) {				mimeType = "image/png";				data = FileStore.FlameImage;				doHeader = false;				if (logdebug)  System.out.println("Image Request...");			}						if (ImportantLine.startsWith("GET /ajaxstyle.css")) {				mimeType = "text/css";				data = FileStore.CSS;				doHeader = false;				if (logdebug)  System.out.println("CSS Request...");			}						if (ImportantLine.startsWith("GET / ") ||				ImportantLine.startsWith("GET /index.html")) {				mimeType = "text/html";				data = FileStore.IndexPage;				doHeader = false;				if (logdebug)  System.out.println("Entry Request...");			}						if (ImportantLine.startsWith("GET / ") ||					ImportantLine.startsWith("GET /failure.html")) {					mimeType = "text/html";					data = FileStore.IndexPage;					doHeader = false;					if (logdebug)  System.out.println("Failure Request...");				}						try {				Date now = new Date();				String FileText = "HTTP/1.1 200 OK\n\r" + "Date: "						+ now.toString() + "\n\rServer: AjaxVNC\n\r"						+ "Last-Modified: " + now.toString()						+ "\n\rAccept-Ranges: bytes" + "\n\rConnection: close"						+ "\n\rContent-Type: " + mimeType + "\n\n\r";				if (doHeader)					this.putline(FileText);				try {					if (logdebug) System.out.println("Sending Data...");					this.putlines(data);				} catch (Exception e) {				}				if (logdebug) System.out.println("Closing Connection to " + Service);			} catch (Exception e) {				e.printStackTrace();			} finally {				this.stop();			}		}		public void putline(String line) {			putlines(line.getBytes());		}		// 在输出流中写数据,将结果返回去客户端		public void putlines(byte[] data) {			if (data == null)				return;			try {				conn.getOutputStream().write(data);			} catch (SocketException e) {				kicker = null;			} catch (IOException e) {				System.out.println("Putline: " + e);			}		}		/**		 * 获取连接请求参数		 */		public String getline() {			StringBuffer strbuf = new StringBuffer();			int tmp;			do {				try {					tmp = instream.read();				} catch (Exception e) {					// System.out.println("Caught!");					return null;				}				if (tmp == -1)					return (null);				if ((tmp != 0) && (tmp != '\n'))					strbuf.append((char) tmp);			} while (tmp != '\n');			return (strbuf.toString());		}	}	/**	 * 创建Session	 */	public String makeSession() {		Random p = new Random();		StringBuffer b = new StringBuffer(32);		for (int j = 0; j < 32; j++) {			int charNum = p.nextInt(16);			switch (charNum) {			case 0:				b.append("0");				break;			case 1:				b.append("1");				break;			case 2:				b.append("2");				break;			case 3:				b.append("3");				break;			case 4:				b.append("4");				break;			case 5:				b.append("5");				break;			case 6:				b.append("6");				break;			case 7:				b.append("7");				break;			case 8:				b.append("8");				break;			case 9:				b.append("9");				break;			case 10:				b.append("A");				break;			case 11:				b.append("B");				break;			case 12:				b.append("C");				break;			case 13:				b.append("D");				break;			case 14:				b.append("E");				break;			case 15:				b.append("F");				break;			}		}		return b.toString();	}		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -