[英]Java - Is there a way to determine file size from a base64 input?
We have a java program where the input is a base64 string version of the file.我们有一个 java 程序,其中输入是文件的 base64 字符串版本。 Files would be processed differently depending on their sizes so we have to find a way to determine its size based on its base64 input.
文件将根据其大小进行不同的处理,因此我们必须找到一种方法根据其 base64 输入确定其大小。
is there a way to do this?有没有办法做到这一点? I'm thinking of recreating the file from the base64 then get the size but that would mean having to store it temporarily.
我正在考虑从 base64 重新创建文件然后获取大小,但这意味着必须暂时存储它。 We don't want that.
我们不想那样。 Whats the best way to do this?
什么是最好的方法来做到这一点?
We are using Java 8我们正在使用 Java 8
Basically, yes.基本上,是的。 In basis, Base64 encodes 3 bytes using 4 characters.
在基础上,Base64 使用 4 个字符编码 3 个字节。 However, you must tackle 2 additional major issues:
但是,您必须解决另外两个主要问题:
=
sign is used for padding.=
符号用于填充。 Both of these issues can be addressed fairly easily:这两个问题都可以很容易地解决:
It's not hard to loop over a string and increment a counter unless the character at that position in the string is whitespace.除非字符串中该位置的字符是空格,否则循环遍历字符串并增加计数器并不难。
Then multiply by 3 and divide by 4.然后乘以3并除以4。
Then subtract 2 if the string ends in ==
.如果字符串以
==
结尾,则减去 2。 If it ends in =
, subtract 1.如果以
=
结尾,则减 1。
You can count =
signs during your loop.您可以在循环中计算
=
符号。
int countBase64Size(String in) {
int count = 0;
int pad = 0;
for (int i = 0; i < in.length(); i++) {
char c = in.charAt(i);
if (c == '=') pad++;
if (!Character.isWhitespace(c)) count++;
}
return (count * 3 / 4) - pad;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.