[ACCEPTED]-How to handle string encoding in java?-character-encoding

Accepted answer
Score: 12

That is not how you convert between character 24 sets. What you need to be worrying about 23 is this part:


Once you have it as a string, it 22 is stored internally as 16 bit unicode. Getting 21 it as bytes and then telling java to treat 20 those bytes as if they were UTF-8 is not 19 going to do anything good.

If you found it 18 to be fine, that is just a coincidence. Once 17 you call that getParameter("world").toString() you 16 have your unicode string. The further decoding 15 and encoding will just break certain characters, it 14 just happens to not break yours.

The question 13 is how you get that attribute to display 12 later? You say the jsp page's encoding is 11 not unicode, but rather Big5, so what are 10 you doing to get that string out of the 9 attribute map and put it on that page? That 8 is the likely source of the problem. Given 7 the misunderstanding about how to handle 6 the character conversion in getting the 5 parameter, it would be likely that there 4 are some mistakes on that Big5 page as well.

By 3 the way, do you really need to use Big5? Would 2 UTF-16 work (if not UTF-8)? It could certainly 1 remove some headaches.

Score: 0

The way I handle encodings in Java is by 6 not allowing text encoded in something other 5 than UTF-8 to be uploaded to my site. This 4 is how I do it:

try {
    CharsetDecoder charsetDecoder = StandardCharsets.UTF_8.newDecoder();

    return IOUtils.toString(new InputStreamReader(new FileInputStream(filePath), charsetDecoder));
catch (MalformedInputException e) {
    // throw an exception saying the file was not saved with UTF-8 encoding.

I recommend reading https://www.baeldung.com/java-char-encoding. It 3 contains a very good summary of what you 2 need to know regarding String encoding in 1 Java.

More Related questions