4

Im trying to decode a string that was genearated by:

Javascript code:

fileReader.readAsDataURL(fileToLoad);

Ps.: Its a part of encode a file.

After get the file is encoded, i put inside of Json and send to a restfull service using POST method.

Java code (restfull):

String radiationFilePath = json.getString("radiationFilePath");
String newRadFile = radiationFilePath.replace("\\", ""); \\I read that it is a needed because JsonObject add some '\'
byte[] radiationFileAsBytes = Base64.getDecoder().decode(newRadFile);

Doing that, im receiving an exception:

java.lang.IllegalArgumentException: Illegal base64 character 3a

What should i do?

PS.: Im using Maven to import dependencies

KpsLok
  • 853
  • 9
  • 23

1 Answers1

6

actually I just had the same issue. here's how I solved it.

First you don't need to do this : String newRadFile = radiationFilePath.replace("\", "");

But you have to to this instead String newRadFile = radiationFilePath.split(",")[1]

To solve it I just used byte[] data = Base64.decodeBase64(newRadFile) from org.apache.commons.codec.binary.Base64 instead of Base64.getDecoder().decode(newRadFile);

Then if you want to create a file from your byte array you can use FileUtils.writeByteArrayToFile(new File("test.jpg"), data) from org.apache.commons.io.FileUtils;

hope it helps, Adrien.

  • If the file has more than 100kb, the file content is not according with original file... Why it happend? – KpsLok Feb 23 '16 at 19:00