JAVA基础(21) java代码替换指定url里的参数值

1.问题如下;

已知字符串url ="adminHeanderMenu.do/action=menu&processId=6898&t=5454654654654",java后台代码发现参数processId里的值不是想要的,需要替换成"6547"?

2.解决办法:

直接上代码吧,SO easy!

  方法一:

/**
	 * java解析代码
	 * 
	 * @param url
	 * @param name
	 * @param accessToken
	 * @return
	 */
	public static String replaceAccessToken(String url, String name, String accessToken) {
		if (StringUtils.isNotBlank(url) && StringUtils.isNotBlank(accessToken)) {
			int index = url.indexOf(name + "=");
			if (index != -1) {
				StringBuilder sb = new StringBuilder();
				sb.append(url.substring(0, index)).append(name + "=").append(accessToken);
				int idx = url.indexOf("&", index);
				if (idx != -1) {
					sb.append(url.substring(idx));
				}
				url = sb.toString();
			}

		}
		return url;
	}

方法二:

/**
	 * 正则替换
	 * @param url
	 * @param name
	 * @param accessToken
	 * @return
	 */
	public static String replaceAccessTokenReg(String url, String name, String accessToken) {
		if (StringUtils.isNotBlank(url) && StringUtils.isNotBlank(accessToken)) {
			url = url.replaceAll("(" + name + "=[^&]*)", name + "=" + accessToken);
		}
		return url;
	}

测试类:

public static void main(String[] args) {
		String test_str = "adminHeanderMenu.do/action=menu&processId=6898&t=5454654654654";
		System.out.println("str的长度" + test_str.length());
		//调用方法
		String replace_str ="processId"; //需要替换的参数
		String replace_val = "6547"; //需要替换的值
		
		String result1 = replaceAccessToken(test_str, replace_str, replace_val);  
		System.out.println("result1="+result1);
		
                String result2 = replaceAccessTokenReg(test_str, replace_str, replace_val);
		System.out.println("result2="+result2);
	
	}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是一个使用 Java 代码打开浏览器并访问指定 URL 的示例: ``` import java.awt.Desktop; import java.net.URI; public class OpenBrowser { public static void main(String[] args) { String url = "https://www.example.com"; if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { try { Desktop.getDesktop().browse(new URI(url)); } catch (Exception e) { e.printStackTrace(); } } } } ``` 这段代码使用 `java.awt.Desktop` 类和 `java.net.URI` 类,首先检查当前操作系统是否支持 `Desktop` 功能,然后调用 `Desktop.getDesktop().browse` 方法打开浏览器并访问指定 URL。 ### 回答2: 要实现用Java代码打开浏览器并唤起指定URL的接口,可以使用Java的Desktop类来实现。以下是代码示例: ```java import java.awt.*; import java.net.URI; public class OpenBrowserInterface { public static void openURL(String url) { try { Desktop desktop = Desktop.getDesktop(); if (desktop.isSupported(Desktop.Action.BROWSE)) { desktop.browse(new URI(url)); } else { throw new UnsupportedOperationException("Desktop browsing is not supported."); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { String targetURL = "https://www.example.com"; // 替换为要打开的URL openURL(targetURL); } } ``` 在上述代码中,我们定义了一个openURL方法,该方法接收一个URL字符串作为参数。在方法内部,我们首先获取当前操作系统的桌面实例,然后通过调用browse方法来打开浏览器并跳转到指定URL。 在main方法中,我们通过传递需要打开的URL字符串调用openURL方法,从而实现了打开浏览器并唤起指定URL的接口。 需要注意的是,此代码需要在支持桌面功能的操作系统上才能正常运行。如果当前操作系统不支持桌面功能,会抛出UnsupportedOperationException异常。 ### 回答3: 要实现通过Java代码打开浏览器并唤起指定URL的接口,可以使用Java的Desktop类。以下是一个简单的例子: ```java import java.awt.Desktop; import java.net.URI; public class BrowserLauncher { public static void openURL(String url) { try { Desktop.getDesktop().browse(new URI(url)); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { String url = "https://www.example.com"; // 指定URL openURL(url); } } ``` 在上述代码中,我们定义了一个`BrowserLauncher`类,其中包含了一个名称为`openURL`的静态方法。该方法接收一个字符串参数`url`,即我们想要打开的URL。在`openURL`方法内部,我们通过调用`Desktop.getDesktop().browse(new URI(url))`来唤起默认浏览器并打开指定URL。如果有其他的异常发生,我们在`catch`块中打印了异常堆栈跟踪信息。 在`main`方法中,我们调用`openURL`方法并传入我们想要访问的URL作为参数。这样,当我们运行这段代码时,就会打开默认浏览器并跳转至指定URL。 请注意,`Desktop`类不一定适用于所有的操作系统和Java开发环境,所以在使用这段代码之前,请确保你的环境支持。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值