英文:
Decode a Base64 Java encoded string in Javascript
问题
以下是已翻译的内容:
在Java中将字符串编码为byte[]
:
byte[] head = ("data:image/" + FilenameUtils.getExtension(file.getName()) + ";base64,").getBytes();
byte[] data = Base64.getEncoder().encode(Files.readAllBytes(file.toPath()));
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
byteStream.write(head);
byteStream.write(data);
return Base64.getEncoder().encode(byteStream.toByteArray());
如何在客户端使用JavaScript解码它?
我尝试使用atob
,但它没有给我正确的字符串:
atob(javaEncodedByteArray);
我可以在Java中正常解码它:
Base64.getDecoder().decode(javaEncodedByteArray);
但我找不到如何在JavaScript中做到这一点。
这是一个示例数据:
Base字符串:
data:image/;base64,/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAyADIDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYW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zM+yt9Oj+IFoJri6ivF1OPdAljGkav5g+UbZSAM8cZ/Gve68N0GHxKPEWmGfw75UIu4vMk/sZE2LvGTu8
<details>
<summary>英文:</summary>
I encoded a string to a `byte[]` with Java:
```Java
byte[] head = ("data:image/" + FilenameUtils.getExtension(file.getName()) + ";base64,").getBytes();
byte[] data = Base64.getEncoder().encode(Files.readAllBytes(file.toPath()));
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
byteStream.write(head);
byteStream.write(data);
return Base64.getEncoder().encode(byteStream.toByteArray());
How can I decode it client side, with Javascript?
I tried using atob
, but it doesn't give me the correct string:
atob(javaEncodedByteArray);
I can decode it fine in Java with:
Base64.getDecoder().decode(javaEncodedByteArray);
But I can't find how to do it in Javascript.
Here is an example data:
Base string:
data:image/;base64,/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAyADIDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVY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2VPSTJieGhyTVUybFg5MUROSmJ5STl1MnpCV0hIVW93UDN2MHIwTXRkcXI5UDFSd1ppcjBsNi9vem5mQzJwWGk2enBHbUNkaFpOcWx2T1l1TUZ3d0dmWHAvVDBwbXI2bmV3SytuUlhEcmFYTnJadExFT2pGWUl5UDgrdzlCVm5UMWcwN1VyVytpOE82dzBsdE1reXExd01FcVFRRCs1NmNWSE5FbDJZcExuUU5XTXl3eFJNWTV0cW5ZaXBrQXhIR1F2cWE5alQybk5iVDVibmthOG5MZlg1N0V2aEgvQUkrYlAvc05XSDg1YStocThFMEdJeDZ2cGx0YmFOcVVDdHFsck5KTE8rOEFJeEhhTmNmZkp6bnRYdmRlVG1idk5NOVRMVmFEUVVVVVY1cDZRVjRiNCtpMHh2SDErWjd5N1M0Sml5aVdhU0xueTB4eVpCbnQxRmU1VjQ5NDNoOFFONHd2elk2RjlxdGo1ZXliK3lVbTNmdTF6ODVRazg1SFhqR0s3OHVkcXIxdHAva2NPWUs5SmFYMS93QXpsTmF0dE5UV3J4YjI1dTdlNUVwRWtNT25SS2lIMEFXYkdLcTI5dm96WE1RanY3OTVDNENxMm54a0U1NEJCbXgrZGRScDNodnhqcVVSbFhSZE50MDV4OXAwKzNqSkk3YlNtZjB4VDV2QzNqU0N6TncyaTZXeFhKYUpMTzFad0Izd0Y1K2d5ZmF2VlZhSzkzblgzci9JOHQwVy9lNUg5ei96TSt5dDlPaitJRm9Kcmk2aXZGMU9QZEFsakdrYXY1ZytVYlpTQU04Y1ovR3ZlNjhOMEdIeEtQRVdtR2Z3NzVVSXU0dk1rL3NaRTJMdkdUdThzYmNEdm5pdmNxOHpNRythTjNmUTlMTDB1V1ZsYlVLS0tLODQ5QUtLS0tBQ2lpaWdBb29vb0FLS0tLQVAvOWs9
答案1
得分: 1
我看到你编码的方式有误。你应该使用 `Base64.getEncoder().encodeToString()`。
示例:
在Java中进行编码:
```java
import java.nio.charset.StandardCharsets;
import java.util.*;
String myName = "roger";
String encodeString = Base64.getEncoder().encodeToString(myName.getBytes(StandardCharsets.UTF_8));
// 输出:cm9nZXI=
在JS中进行解码:
atob(encodeString);
// atob("cm9nZXI=") -> 输出:"roger"
<details>
<summary>英文:</summary>
I see you encode wrong way. You should use `Base64.getEncoder().encodeToString()`
Example:
Encode in Java
import java.nio.charset.StandardCharsets;
import java.util.*;
String myName = "roger";
String encodeString = Base64.getEncoder().encodeToString(myName.getBytes(StandardCharsets.UTF_8));
// output: cm9nZXI=
Decode in JS:
atob(encodeString);
// atob("cm9nZXI=") -> output: "roger"
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论