差異處
這裏顯示兩個版本的差異處。
Both sides previous revision 前次修改 下次修改 | 前次修改 | ||
java:basic:process:parameter_with_double_quotes [2017/02/16 18:11] tony |
java:basic:process:parameter_with_double_quotes [2023/06/25 09:48] (目前版本) |
||
---|---|---|---|
行 2: | 行 2: | ||
====== Launch a process with a parameter has double quotes ====== | ====== Launch a process with a parameter has double quotes ====== | ||
===== Problem ===== | ===== Problem ===== | ||
- | 最近因為FW team把登入密碼限制給拿掉了,而讓我們需要去面對密碼中有特殊字元的問題,其中最難搞的問題就是double-quotes。當你需要執行外部程式時,如果參數有特殊字元你要怎麼處理? | + | 最近因為FW team把登入密碼限制給拿掉了,使得我們需要去面對密碼中有特殊字元的問題,其中最難搞的問題就是double-quotes。當你需要執行外部程式時,如果參數有特殊字元你要怎麼處理? |
===== How to? ===== | ===== How to? ===== | ||
- | 如果是下command line,在linux上的雙引號可以這樣處理: | + | 這問題是會與OS有關的。如果是下command line,在linux上的雙引號可以這樣處理: |
<code bash> | <code bash> | ||
'xxx"xxx' | 'xxx"xxx' | ||
行 23: | 行 23: | ||
List<String> param = new ArrayList<>(); | List<String> param = new ArrayList<>(); | ||
for( String token : aParams ){ | for( String token : aParams ){ | ||
- | param.add(token.replace("\"", "\"\"")); | + | if(token.contains("\"")){ |
+ | token = StringUtil.apendStrings("\"", token.replace("\"", "\"\""), "\""); | ||
+ | } | ||
+ | param.add(token); | ||
} | } | ||
return param; | return param; | ||
行 32: | 行 35: | ||
} | } | ||
</code> | </code> | ||
+ | 首先是如果字串內有雙引號,就需要透過雙引號去跳脫;但如果雙引號是在前後,會導致PrcoessBuilder不會再額外+雙引號上去,所以client要先+上去。 | ||
===== commons-cli ===== | ===== commons-cli ===== | ||
- | 除了ProcessBuilder,我們還遇到commons-cli parse參數中,包含double-quotes的問題。在1.2版本上,如果你的參數為: | + | 除了ProcessBuilder,我們還遇到commons-cli parse參數中,如果包含double-quotes會被消掉的問題。在1.2版本上,如果你的參數為: |
<code> | <code> | ||
"test" | "test" |