目前我面臨一個問題,即控制臺中輸出的 URL 與導航欄中顯示的實際 URL 不同。我看看我有正則運算式的值,它是正確的,但是當放入 IE 時,它是不同的。導航欄中的 URL 將僅是控制臺中顯示的內容,a=test而不是a=test&c=import&f=&uid=user1哪個。我可以知道如何解決這個問題嗎?謝謝。
這是它的代碼。
UserID user = new UserID();
public static void main(String[] args) {
String newUrl = replaceUserID(url);
System.out.print("cmd.exe /c start iexplore -k " newUrl);
try{
Process p = Runtime.getRuntime().exec("cmd.exe /c start iexplore " newUrl);
try{
p.waitFor();
}
catch( InterruptedException ie ){
System.out.println("InterruptedException " ie.getMessage());
}
InputStream err = p.getErrorStream();
int ctr = 0;
if ( (ctr = err.available()) > 0 ){
byte[] buf = new byte[ctr];
System.out.println("Process failed with error:\n" new String(buf, 0, ctr));
}
}
catch(IOException ioe)
{
System.out.println("InterruptedException " ioe.getMessage());
}
}
public static String checkUserID(){
String username = System.getenv("USERNAME"); //grab user login id for windows
return username;
}
public static String replaceUserID(String oldUrl) {
String params = "http://example.com?a=test&c=import&f=&uid=userid";
String username = checkUserID();
try {
Pattern p = Pattern.compile("uid=([^&%] )");
Matcher m = p.matcher(params);
while (m.find()) {
// System.out.println(m.group(1).toString());
if(m.group(1).toString() != username) {
//replace url paremeters with correct userID.
String newUrl = params.replace(m.group(1).toString(), username);
return newUrl;
}
else {
System.out.println("The username is the same");
return params;
}
}
} catch (PatternSyntaxException ex) {
// error handling
ex.printStackTrace();
}
return oldUrl;
}
uj5u.com熱心網友回復:
&對命令列有特殊意義(concat 命令,請參見此處:如何在 Windows CMD 中在一行中運行兩個命令?)。
您需要轉義 url 或整個命令。如果我沒記錯的話,你需要在它周圍加上雙引號:"...iexplore -k \"" newUrl "\""
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/346361.html
上一篇:生成大于和小于的字串
