[英]How do I retain the encoding in argument when using MessageFormat in Java
我正在嘗試按以下方式使用MessageFormat
,
String downloadsUrl = "http://host/downloads?tags={0}";
Object[] formatArgs = {"sequence%20diagram"};
String url = new MessageFormat(downloadsUrl).format(formatArgs);
但是,當我查看最終的URL字符串時,它是http:// host / downloads?tags = sequence圖
是否可以保留%20並且沒有MessageFormat用空格替換?
您提供的代碼未添加上面代碼返回的空間“ http:// host / downloads?tags = sequence%20diagram”
您的目標servlet正在進行替換。 映射到“ / downloads”的任何內容都是解析標簽參數並執行url解碼。 您可以按以下方式重構可能的編碼。 您將需要在下面處理UnsupportedEncodingException。
String encoded = URLEncoder.encode( request.getParameter( name ), "UTF8" );
不幸的是,這只是一種可能的編碼,默認情況下會將空格轉換為“ +”。 要取回“%20”,您需要訴諸
encoding = encoding.replaceAll( "+", "%20" );
這可能對您有用。 通常,更可取的是對解碼值而不是編碼值進行歸一化,因為每個解碼值有許多可能的編碼。
基於此,我將猜測在值周圍加上單引號是可行的...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.