java 如何使用 GWT 将短字符串编码/解码为 Base64?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2250421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:18:13  来源:igfitidea点击:

How do I encode/decode short strings as Base64 using GWT?

javagwtbase64

提问by David Tinker

I need to encode a short String as base 64 in GWT and decode the base 64 string on the server. Anyone have utility class or library for this?

我需要在 GWT 中将一个短字符串编码为 base 64 并在服务器上解码 base 64 字符串。任何人都有实用程序类或库吗?

采纳答案by Janus Troelsen

You can use native JavaScript for this on the client on all browsers except IE ≤ 9. On the server you can use one of the official classes.

您可以在除 IE ≤ 9 之外的所有浏览器上的客户端上使用本机 JavaScript。在服务器上,您可以使用官方类之一

Java/GWT:

Java/GWT:

private static native String b64decode(String a) /*-{
  return window.atob(a);
}-*/;

Encode is btoa.

编码为btoa.

回答by Jake W

You can use the BaseEncoding class provided by Guava.

您可以使用 Guava 提供的 BaseEncoding 类。

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/BaseEncoding.html

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/BaseEncoding.html

For example:

例如:

try {
  String encoded = BaseEncoding.base64().encode("foo".getBytes("UTF-8"))
} catch (UnsupportedEncodingException e) {
  GWT.log(e.getMessage());
}

And don't forget to add the following line to your GWT module XML:

并且不要忘记将以下行添加到您的 GWT 模块 XML:

<inherits name="com.google.common.io.Io"/>

The BaseEncoding class can be used on both the GWT client side and server side.

BaseEncoding 类可用于 GWT 客户端和服务器端。

回答by Ronan Quillevere

You can have a look at https://github.com/mooreds/gwt-crypto

你可以看看https://github.com/mooreds/gwt-crypto

It provides base64 encoding to GWT.

它为 GWT 提供 base64 编码。

Base64.encode(string.getBytes());

Add the import below :

添加下面的导入:

import com.googlecode.gwt.crypto.bouncycastle.util.encoders.Base64;

Don't forget to add the following line to your GWT module XML:

不要忘记将以下行添加到您的 GWT 模块 XML 中:

<inherits name="com.googlecode.gwt.crypto.Crypto"/>    

Maven dependency

Maven 依赖

<dependency>
    <groupId>com.googlecode.gwt-crypto</groupId>
    <artifactId>gwt-crypto</artifactId>
    <version>2.3.0</version>
</dependency>

回答by Brandon

Base64 class can't be used on the client side. It would have to be emulated.

Base64 类不能在客户端使用。它必须被模仿。