简体   繁体   中英

How can i decode in java a string generated by javascript using readAsDataURL() and sent by Json?

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

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.

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