我如何在 java 中解码由 java 脚本使用 readAsDataURL() 生成并由 Json 发送的字符串?

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

我正在尝试解码生成的字符串:

Java脚本代码:

fileReader.readAsDataURL(fileToLoad);

Ps.: 它是编码文件的一部分。

获取文件编码后,我放入 Json 并使用 POST 方法发送到 restfull 服务。

Java代码(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);

这样做,我收到一个异常:

java.lang.IllegalArgumentException: Illegal base64 character 3a

我该怎么办?

PS.: 我正在使用 Maven 导入依赖项

其实我也遇到了同样的问题。这是我解决它的方法。

首先你不需要这样做: String newRadFile = radiationFilePath.replace("\", "");

但你必须这样做 字符串 newRadFile = radiationFilePath.split(",")[1]

为了解决这个问题,我只使用了 byte[] data = Base64.decodeBase64(newRadFile) from org.apache.commons.codec.binary.Base64代替 Base64.getDecoder().decode(newRadFile);

然后如果你想从你的字节数组创建一个文件,你可以使用 FileUtils.writeByteArrayToFile(new File("test.jpg"), data) from org.apache.commons.io.FileUtils;

希望对您有所帮助, 阿德里安.