首页 > 解决方案 > 与控制台相比,导航中显示的输出和实际 url 不同

问题描述

目前我面临一个问题,即控制台中输出的 URL 与导航栏中显示的实际 URL 不同。我看看我有正则表达式的值,它是正确的,但是当放入 IE 时,它是不同的。导航栏中的 URL 将仅a=test代替a=test&c=import&f=&uid=user1控制台中显示的 URL。我可以知道如何解决这个问题吗?谢谢。

这是它的代码。

    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;
    }

标签: javacmd

解决方案


&对命令行有特殊的意义(concat 命令,请看这里:How do I run two commands in one line in Windows CMD?)。

您需要转义 url 或整个命令。如果我没记错的话,你需要在它周围加上双引号:"...iexplore -k \"" + newUrl + "\""


推荐阅读