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

曾经用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;
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值