简体   繁体   中英

Cannot decode special character when using `IOUtils.toString(containerRequestContext.getEntityStream(),“UTF-8”); `

I use IOUtils.toString(containerRequestContext.getEntityStream(),"UTF-8"); to get the entity stream and use IOUtils to transfer it into string. However, when meeting with some special character like "{", "[" and etc, IOUtils cannot decode them, only english and numbers and "&" can be decode.

The content of the entity decoded by IOUtls is as following

S=9&X=5&R=5&command=%5B%7B++++++++++%22zoneCode%22%3A+%22R98542%22%2C+++++++++++%22targetPosX%22%3A+4122.54%2C+++++++++++%22targetPosY%22%3A+9547.76%2C+++++++++++%22targetPosZ%22%3A+12548.69%2C+++++++++++%22power%22%3A+521456%2C+++++++++++%22duration%22%3A+6412+++++++%7D%2C+++++++%7B+++++++++++%22zoneCode%22%3A+%22R652485%22%2C+++++++++++%22targetPosX%22%3A+95482.36%2C+++++++++++%22targetPosY%22%3A+7845.85%2C+++++++++++%22targetPosZ%22%3A+9847.37%2C+++++++++++%22power%22%3A+741785%2C+++++++++++%22duration%22%3A+6482+++++++%7D%2C++++++%7B+++++++++++%22zoneCode%22%3A+%22R742545%22%2C+++++++++++%22targetPosX%22%3A+16982.93%2C+++++++++++%22targetPosY%22%3A+85623.17%2C+++++++++++%22targetPosZ%22%3A+4872.27%2C+++++++++++%22power%22%3A+34528%2C+++++++++++%22duration%22%3A+342+++++++%7D++%5D+

The primitive form data is as below: |field|value| |-|-| |S|9| |X|5| |R|5| |command|(text of a json array)|

The value of "command" is shown as below:

[
{          
"zoneCode": "R98542",           
"targetPosX": 4122.54,           
"targetPosY": 9547.76,           
"targetPosZ": 12548.69,           
"power": 521456,           
"duration": 6412       
},       
{          
"zoneCode": "R652485",          
"targetPosX": 95482.36,           
"targetPosY": 7845.85,           
"targetPosZ": 9847.37,           
"power": 741785,           
"duration": 6482       
},      
{           
"zoneCode": "R742545",           
"targetPosX": 16982.93,           
"targetPosY": 85623.17,           
"targetPosZ": 4872.27,           
"power": 34528,           
"duration": 342       
}  
] 

You should also do a URL Decode. Import the java.net.URLDecoder package and use the URLDecoder.decode(string, encoding) method.

See an example below (byteToString variable is the text from your question):

public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
        String byteToString = "S=9&X=5&R=5&command=%5B%7B++++++++++%22zoneCode%22%3A+%22R98542%22%2C+++++++++++%22targetPosX%22%3A+4122.54%2C+++++++++++%22targetPosY%22%3A+9547.76%2C+++++++++++%22targetPosZ%22%3A+12548.69%2C+++++++++++%22power%22%3A+521456%2C+++++++++++%22duration%22%3A+6412+++++++%7D%2C+++++++%7B+++++++++++%22zoneCode%22%3A+%22R652485%22%2C+++++++++++%22targetPosX%22%3A+95482.36%2C+++++++++++%22targetPosY%22%3A+7845.85%2C+++++++++++%22targetPosZ%22%3A+9847.37%2C+++++++++++%22power%22%3A+741785%2C+++++++++++%22duration%22%3A+6482+++++++%7D%2C++++++%7B+++++++++++%22zoneCode%22%3A+%22R742545%22%2C+++++++++++%22targetPosX%22%3A+16982.93%2C+++++++++++%22targetPosY%22%3A+85623.17%2C+++++++++++%22targetPosZ%22%3A+4872.27%2C+++++++++++%22power%22%3A+34528%2C+++++++++++%22duration%22%3A+342+++++++%7D++%5D+";
        String urlDecoded = URLDecoder.decode(byteToString , StandardCharsets.UTF_8.toString());
        System.out.println(urlDecoded);
    }

Result:

S=9&X=5&R=5&command=[{          "zoneCode": "R98542",           "targetPosX": 4122.54,           "targetPosY": 9547.76,           "targetPosZ": 12548.69,           "power": 521456,           "duration": 6412       },       {           "zoneCode": "R652485",           "targetPosX": 95482.36,           "targetPosY": 7845.85,           "targetPosZ": 9847.37,           "power": 741785,           "duration": 6482       },      {           "zoneCode": "R742545",           "targetPosX": 16982.93,           "targetPosY": 85623.17,           "targetPosZ": 4872.27,           "power": 34528,           "duration": 342       }  ] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM