String myString = "some cyrillic text";
byte bytes[] = type.getBytes("UTF-8");
Now bytes
contains a UTF-8 encoding of the string. If you were to call new String(bytes, "UTF-8")
you would get back an equivalent string to the original one.
But ...
String value = URLEncoder.encode(
new String(bytes, "Windows-1251"), // HERE
"Windows-1251");
... at HERE
you are decoding with the wrong character encoding. The String
constructor takes your word for it ... and the result is mangled characters.
Understand this:
The bytes
array contains just the encoded text. It doesn't contain anything to identify the encoding scheme. So the String
constructor has no way of knowing what the correct encoding is ... apart from what you tell it. And it has no (reliable) way of knowing if the encoding you told it is correct. Let alone fixing your mistake.
The correct way to do what your code does is this:
String myString = "some cyrillic text";
String value = URLEncoder.encode(myString, "Windows-1251");
However ... we don't have sufficient context to know whether that is what is actually required for your application.