Javascript base64 encode java decode

2.6k Views Asked by At

I got this csv file that get's base64 encoded using javascript

SomeField1;SomeField2;SomeField3
Value1;Value2;Value3
Value1;ÀÃÁÄÑ;Value3
Value1;Value2;Value3

This is the javascript that is used to encode it

var openFile = function(event) {
    var input = event.target;

    var reader = new FileReader();
    reader.onload = function(){
        var text = reader.result.substring(reader.result.indexOf(",") + 1);

        // From string >> encoded  string
        console.log(text);
        document.getElementById('demo').innerHTML = text

    };
    reader.readAsDataURL(input.files[0],'CP-1252');
    var input = event.target;

};

After it get's send it is decoded in java like this

public static void GetLinesCSV(String[] Base64str, ArrayList list) {
    byte[] baos = DatatypeConverter.parseBase64Binary(Base64str[0]);
    String file_string = new String(baos);

    baos = Base64_2.decode(Base64str[0]);

    file_string = new String(baos);

    String file_out = "";
    String[] tokens = file_string.split("\r\n", -1);

    for (int i = 1; i < (tokens.length - 1); i++) {
        list.add(tokens[i]);
    }
}

Problem is the csv file can contain special characters like Ä or Ñ like the example. Then i get this as output.

SomeField1;SomeField2;SomeField3
Value1;Value2;Value3
Value1;ÀÃ�ÄÑ;Value3
Value1;Value2;Value3

If I encode and decode both in javascript I get the incorrect output, if I encode and decode in java I also get the incorrect output... I changed settings so eclipse uses windows-1252 as well. Am I forgetting something or is it not possible to encode and decode in different languages using the same charset?

0

There are 0 best solutions below