HardBirch

Applet在web页面打开远程桌面前端测试代码【备忘】

时间:12-07-04 栏目:iOS移动应用开发技术 作者:张飞不张,文采横飞 评论:4 点击: 3,455 次

曾经用Applet在web页面打开远程桌面的前端测试代码,后台代码无法公开,仅作备忘。

RdpApplet.java:

package com.elusiva.rdp.applet;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.TextArea;
import java.io.ByteArrayOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import com.elusiva.rdp.Common;
import com.elusiva.rdp.OrderException;
import com.elusiva.rdp.RdesktopException;
import com.elusiva.rdp.RdesktopSwing;

public class RdpApplet extends Applet {
	private final String rdpShellPath = "C:\Program Files\XXX\Open Virtual Desktop\rdpshell.exe";
	private RdpThread_2 rThread = null;
	private String[] argArray = null;
	private List<String> argList = new ArrayList<String>();
    TextArea aTextArea = null;
    PrintStream aPrintStream  = null;

   public void paint(Graphics g){
       g.setColor(new Color(0xFFFFFF));
       g.fillRect(0,0,g.getClipBounds().width,g.getClipBounds().height);
       g.setColor(new Color(0x000000));
       int width = g.getFontMetrics().stringWidth("Launching isoft IVAPP Everywhere session...");
       int x = (int) (g.getClipBounds().getWidth() / 2) - (width/2);
       int y = (int) (g.getClipBounds().getHeight() / 2);
       if(!redirectOutput) g.drawString("Launching isoft IVAPP Everywhere session...", x, y);
       width = g.getFontMetrics().stringWidth("Connect to Server...");
       x = (int) (g.getClipBounds().getWidth() / 2) - (width/2);
       y = (int) (g.getClipBounds().getHeight() / 2) + 20;
       if(!redirectOutput) g.drawString("Connect to Server...", x, y);
   }

   boolean redirectOutput = false;

   public void init(){
       redirectOutput = isSet("redirectOutput");
       if(redirectOutput){
           aPrintStream = new PrintStream( new FilteredStream(new ByteArrayOutputStream() ) );
           System.setOut(aPrintStream);
           System.setErr(aPrintStream);
           aTextArea = new TextArea();
           setLayout(new BorderLayout());
           add("Center" , aTextArea);
       }

       initArgArray();
       //getargArray();
   }

	public void initArgArray() {
		String url = this.getParameter("url");
		System.out.println("url=" + url);
		Map<String, String> params = null;
		params = RequestServer.doRequest(url);
		// 获取用户账号参数
		argList.add("-u");
		argList.add("31" + params.get("loginName"));
		// 获取用户密码参数
		argList.add("-p");
		argList.add(params.get("loginPassword"));

		argList.add("-fsmc");
		//获取应用程序path
		argList.add("-s");
		argList.add(rdpShellPath+" "+params.get("appExePath"));

		//获取IP参数
		argList.add(params.get("ip"));
		argArray = new String[argList.size()];
		Object[] objList = argList.toArray();
		int j = 0;
		for (Object o : objList) {
			argArray[j++] = o.toString();
			System.out.print(o.toString());
		}
	}

    public void start(){
        Common.underApplet = true;
        rThread = new RdpThread_2(argArray, this.getParameter("redirect_on_exit"), this);
        rThread.start();
    }

    public void stop(){
        rThread = null;
        notify();
    }

    private boolean isSet(String parameter){
        String s = this.getParameter(parameter);
        if(s != null){
            if(s.equalsIgnoreCase("yes"))
                return true;
        }
        return false;
    }

    class FilteredStream extends FilterOutputStream {
        public FilteredStream(OutputStream aStream) {
           super(aStream);
           }

        public void write(byte b[]) throws IOException {
           String aString = new String(b);
           aTextArea.append(aString);
           }

        public void write(byte b[], int off, int len) throws IOException {
           String aString = new String(b , off , len);
           aTextArea.append(aString);
        }
     }

}

class RdpThread extends Thread{

    String[] args;
    String redirect = null;
    Applet parentApplet = null;

    public RdpThread(String[] args, String redirect, Applet a){
        parentApplet = a;
        this.args = args;
        this.redirect = redirect;
    }

    public void run(){
        this.setPriority(Thread.MAX_PRIORITY);
        try {
            RdesktopSwing.main(args);

            if(redirect != null){
               URL u = new URL(redirect);
               parentApplet.getAppletContext().showDocument(u);
            }
        } catch (OrderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RdesktopException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
            e.printStackTrace();
        }
        Common.underApplet = false;
    }
}

RequestServer.java:

package com.elusiva.rdp.applet;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.Map;

public class RequestServer {
	public static void main(String[] args) throws IOException {
		String requestURL = "http://192.168.210.201/sm/webservices/server_random.php?type=windows&sid=1293712837Q5yTs";
		doRequest(requestURL);
	}

    public static Map<String,String> doRequest(String requestURL) {
    	Map<String,String> params = null;
		try {
			//创建URL
			URL url = new URL(requestURL);

			//打开URL连接
	        URLConnection connection = url.openConnection();  

	        /*
	         * 然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。
	         * 通过把URLConnection设为输出,可以把数据向某个Web页传送,如下:
	         */
	        connection.setDoOutput(true);  

	        // 一旦发送成功,用以下方法就可以得到服务器的回应:
	        InputStream is = connection.getInputStream();  

	        //解析XML获取参数
	        params = XMLReader.ParseXML(is);
		} catch (MalformedURLException e) {
			System.out.println("创建URL失败!");
		} catch(IOException e){
			System.out.println("打开连接失败!");
		}

		return params;
    }
}  

XMLReader.java:

package com.elusiva.rdp.applet;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class XMLReader {
	private final static Map<String,String> params = new HashMap<String,String>();
	public static Map<String,String> ParseXML(InputStream is) {
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse(is);
			//以去掉XML文档中作为格式化内容的空白
			doc.normalize(); 

			NodeList links = doc.getElementsByTagName("server");
			Element link = (Element) links.item(0);

			//将参数放到map中
			String code = link.getElementsByTagName("code").item(0).getFirstChild().getNodeValue();
			if("0".equals(code)){
				params.put("ip", link.getElementsByTagName("ip").item(0)
						.getFirstChild().getNodeValue());
				params.put("loginName", link.getElementsByTagName("loginName").item(0)
						.getFirstChild().getNodeValue());
				params.put("loginPassword", link.getElementsByTagName("loginPassword").item(0)
						.getFirstChild().getNodeValue());
				params.put("appExePath", link.getElementsByTagName("appExePath").item(0)
						.getFirstChild().getNodeValue());
			}
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}

		return params;
	}
}

Applet在web页面打开远程桌面前端测试代码【备忘】:目前有4 条留言

  1. 4楼
    njm21:

    请问你知道properJavaRDP这个工程如何用网页调用吗???
    希望能赐教一下。

    2012-11-10 10:43 [回复]
  2. 地板
    M_ChangGong:

    [quote=njm21]请问你知道properJavaRDP这个工程如何用网页调用吗???
    希望能赐教一下。[/quote]
    写applet

    2012-11-11 21:37 [回复]
  3. 板凳
    njm21:

    [reply]M_ChangGong[/reply]
    我在官网下载的是properJavaRDP-src-1.1这个版本,我发现里面有个net.propero.rdp.applet.RdpApplet.java 这个类。我把这个工程导出为jar包。用网页调用这个jar包,但是发现网页显示Launching properJavaRDP session…
    Connecting to:192.168.1.11 。接着就没有反应了,不知道啥原因。

    2012-11-13 08:51 [回复]
  4. 沙发
    M_ChangGong:

    [quote=njm21][reply]M_ChangGong[/reply]
    我在官网下载的是properJavaRDP-s…[/quote]
    调试一下,可能是没连接上,jar中网络连接部分出问题了。也可能是参数传递的不对吧

    2012-11-13 10:52 [回复]

发表评论


QQ群互动

Linux系统与内核学习群:194051772

WP建站技术学习交流群:194062106

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

优秀程序员,要看优秀书!

赞助商广告

友荐云推荐