我有 xml 文件的 Base64 内容。我想对其进行解码并将文件内容保存到字符串中,而不是保存在外部文件中。 谁能建议这个。 请看下面的内容
Base64编码格式
PD94bUeXBlPmVJQSBjaGFuZ2VzPC9GaWxlVHlwZT4NCiAgICA8RmlsZURhdGU+=
文件内容
<DATA>
<Table>
<LotNo>50700022611201413280</LotNo>
<FileFrom>CAMS Repository Services Limited</FileFrom>
<NoOfRec>2</NoOfRec>
<FileType>eIA partial</FileType>
<FileDate>26/11/2014 13:28:02</FileDate>
<FileFromCode>5</FileFromCode>
<FileToCode>7</FileToCode>
<FileTypeCode>301</FileTypeCode>
</Table>
</Data>
我为此使用以下代码
String outFnm="PD94bUeXBlPmVJQSBjaGFuZ2VzPC9GaWxlVHlwZT4NCiAgICA8RmlsZURhdGU+=";
byte[] b1=outFnm.getBytes();
byte[] bFile=Base64.decodeBase64(b1);
//System.out.println(b1.toString());
//Integer bFile2=Integer.parseInt(bFile);
System.out.println(b1);
System.out.println(bFile.toString());
FileOutputStream fos = new FileOutputStream("D:/test.txt");
fos.write(bFile);
fos.close();
但我不想使用单独的文件来存储它。我想要一个字符串变量来存储“test.txt”内容。
最佳答案
如果你使用的是 java 8,你可以这样做
import java.util.Base64;
byte[] decoded = Base64.getDecoder().decode(encoded);
String s = new String(decoded);
这里encoded
是base64字符串的byte[]数组。
String base64String = "YOUR-BASE64";
byte[] encoded = base64String.getBytes();
关于java - 我有 xml 文件的 Base64 内容。我想对其进行解码并将文件内容保存到字符串中,而不是保存在外部文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31453619/