在 Java 中从 Base 64 字符串转换

发布于 2025-01-07 14:13:33 字数 128 浏览 4 评论 0原文

有没有办法像在 .NET 中使用 Java 一样转换 Base64 字符串?我怎样才能实现这个目标?

这是 .NET 示例:

Convert.FromBase64String

Is there any way to convert a Base64 String like you can in .NET using Java? How can I achieve this?

Here is the .NET example :

Convert.FromBase64String

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟沫凡尘 2025-01-14 14:13:33

我觉得奇怪的是,每次有人询问 Java 中的 Base 64 编码/解码时,给出的唯一答案要么使用 Apache 编解码器库,要么使用不应该使用的旧 com.sun 类,或者假设它在应用程序服务器中比如Tomcat提供的。

自从在 javax.xml.bind.DatatypeConverter 类中引入 Jaxb 1.0 以来,标准 JDK 就提供了 Base 64 支持。

因此,如果您使用的是 Java 6 或更高版本(当 Jaxb1.0 直接添加到 JDK 时,在此之前可以作为参考使用),您不需要使用第 3 方组件来支持 Base 64,您可以使用内置于 JDK 中。

I find it curious that every time someone asks about base 64 encoding/decoding in Java the only answers given are either use the Apache codecs library, use the old com.sun classes which shouldn't be used or assume it is in the application server such as the one provided by Tomcat.

The standard JDK has provided base 64 support since the introduction of Jaxb 1.0 in the javax.xml.bind.DatatypeConverter class.

So if you are using Java 6 or newer (when Jaxb1.0 was added to the JDK directly, was available as a ref prior to that) you don't need to use a 3rd party component for base 64 support, you can use what is built into the JDK.

半衾梦 2025-01-14 14:13:33
  1. 从 Jakara 网站下载“commons-codec-1.3”:Apache Commons Codec
  2. 将 jar 文件放入 lib 文件夹中 将
  3. base64Message 传递给函数以获取解码后的字节

byte[] returnedBytes = Base64.decode(base64Message);
字符串解码字符串=新字符串(decodedBytes);

  1. Download "commons-codec-1.3" from the Jakara web site: Apache Commons Codec
  2. Place jar file in lib folder
  3. Pass the base64Message to the function to get the decoded bytes

byte[] decodedBytes = Base64.decode(base64Message);
String decodedString = new String(decodedBytes);

一直在等你来 2025-01-14 14:13:33

我想你想解码一个base64编码的字符串...你可以使用这个..

BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(yourString.getBytes());

I think you want to decode a base64encoded String...you can use this..

BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(yourString.getBytes());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文