【笔记】Java实现通过本地浏览器打开UClient访问NC服务,如果失败,则打开IE浏览器访问NC Web服务


package nc.baseapp.util;

import java.awt.Desktop;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;


/**
 * <b>启动客户端</b>
 * <p>通过URL打开NC客户端:如果用户安装UClient,则打开UClient;否则打开IE浏览器启动NC客户端。</p>
 * 创建时间: 2020年10月21日 下午12:18:27
 * @author xuzihui
 *
 */
public class BonesBrowserVisitUrl {

	/**
	 * 启动NC客户端
	 * @param ssoKey
	 */
	public static void openUrl(String ncservice_address, String ssoKey){
		//判断是否支持Desktop扩展,如果支持则进行下一步
		if(Desktop.isDesktopSupported()){
			try {
				URI uri = new URI("uclient://start/"+ncservice_address+"?ssoKey="+ssoKey+"&uiloader=nc.login.sso.ui.SSOLoader"); 
				Desktop desktop = Desktop.getDesktop(); //创建desktop对象
				try {
					desktop.browse(uri); //调用默认浏览器打开指定URL
				} catch (Exception e) {
					if(e.getMessage().contains("连到系统上的设备没有发挥作用")){
						openUrlWithIE(ncservice_address + "/login.jsp?ssoKey="+ssoKey);
					} else {
						ExceptionUtils.wrappBusinessException("nc客户端启动失败:" + e.getMessage());
					}
				} 
			} catch (URISyntaxException e) {
				ExceptionUtils.wrappBusinessException("url初始化失败:" + e.getMessage());
			}
			 
		}
	}
	
	/**
	 * 启动IE浏览器启动NC客户端
	 * @param url
	 */
	public static void openUrlWithIE(String url){
		
		//获取操作系统名称
		String osName = System.getProperty("os.name", "");
		if(osName != null){ 
			if(osName.startsWith("Mac OS")){ //苹果系统
				try {
					Class fileMgr = Class.forName("com.apple.eio.FileManager");
					Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[]{String.class});
					openURL.invoke(null, new Object[]{ url });
				} catch (Exception e) {
					ExceptionUtils.wrappBusinessException(e.getMessage());
				}
			} else if(osName.startsWith("Windows")){ //Windows 系统
				try {
					Runtime.getRuntime().exec("cmd /c start iexplore " + url);
				} catch (IOException e) {
					ExceptionUtils.wrappBusinessException(e.getMessage());
				}
			} else { //Unix or Linux系统
				String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
	            		String browser = null;
	            		for (int count = 0; count < browsers.length && browser == null; count++) {
					try {
						// 执行代码,在brower有值后跳出,
						// 这里是如果进程创建成功了,==0是表示正常结束。
						if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0)
							browser = browsers[count];
						if (browser == null)
							throw new Exception("Could not find web browser");
						else
							// 这个值在上面已经成功的得到了一个进程。
							Runtime.getRuntime().exec(new String[] { browser, url});
					} catch (Exception e) {
						ExceptionUtils.wrappBusinessException(e.getMessage());
					}

				}
			}
			
		}
		
	}
	
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值