Java 使用base64编码groovy变量
我有一个这样的字符串,我正试图用base64编码这个字符串Java 使用base64编码groovy变量,java,groovy,Java,Groovy,我有一个这样的字符串,我正试图用base64编码这个字符串 {"htmlBrowserType":"Default","mimeType":"text/html","url":"https://github.comcast.com"} String base64Config = {"htmlBrowserType\":\"Default\",\"mimeType\":\"text/html\",\"url\":\"https://github.comcast.com"} 实际上它是一个g
{"htmlBrowserType":"Default","mimeType":"text/html","url":"https://github.comcast.com"}
String base64Config = {"htmlBrowserType\":\"Default\",\"mimeType\":\"text/html\",\"url\":\"https://github.comcast.com"}
实际上它是一个groovy代码
def encoded = base64Config.bytes.encodeBase64().toString()
使用工具进行编码时,我得到
eyJodG1sQnJvd3NlclR5cGUiOiJEZWZhdWx0IiwibWltZVR5cGUiOiJ0ZXh0L2h0bWwiLCJ1cmwiOiJodHRwczovL2dpdGh1Yi5jb21jYXN0LmNvbSJ9
但是它不能用java代码工作,而我解码的字符串得到的结果是上面的java代码不是
{"htmlBrowserType":"Default","mimeType":"text/html","url":"https://github.comcast.com"}
您的
base64Config
不是字符串,而是闭包:
String base64Config = {"htmlBrowserType\":\"Default\",\"mimeType\":\"text/html\",\"url\":\"https://github.comcast.com"}
应该是:
String base64Config = "{\"htmlBrowserType\":\"Default\",\"mimeType\":\"text/html\",\"url\":\"https://github.comcast.com\"}"
您使用什么Java代码来解码字符串?你给我们看的只是故事的一半。您得到的输出是什么?当我解码输出时,我得到的是'com.uegGlue$\u base64\u configuration\u生成的\u带有\u url\u参数\u设置为\u_closure1@23c1e787'您要编码的Groovy代码看起来不错,但我们需要查看您用来解码的代码。我猜这就是问题所在。我总是更喜欢
{“htmlBrowserType”:“Default”,“mimeType”:“text/html”,“url”:https://github.comcast.com“}”
所以您不需要escaping@tim_yates是的,我同意