使用groovy从SOAPUI中的JSON内容解码base64字符串
我从RESTAPI得到一个json响应,如下所示使用groovy从SOAPUI中的JSON内容解码base64字符串,groovy,soapui,Groovy,Soapui,我从RESTAPI得到一个json响应,如下所示 { "parentnode1": { "childnode1": "abc12345-123-1234-1234-64a0251575f9", "childnode2": "VAL1", "childnode3": "format/pdf", "childnode4": "name.pdf", "base64content": "JVBERi0xLjUNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBl
{
"parentnode1": {
"childnode1": "abc12345-123-1234-1234-64a0251575f9",
"childnode2": "VAL1",
"childnode3": "format/pdf",
"childnode4": "name.pdf",
"base64content": "JVBERi0xLjUNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwRU9G"},
"messages": "This is a message"
}
我想解码
“base64content”
的值,然后将其转换为pdf文件并保存到本地目录。这在SOAP UI和Groovy中可能吗?我提出了这个Groovy脚本,但没有在SOAP UI中测试它(从未使用过它)。试试这个,让我知道会发生什么:
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText (json_response)
String content_decoded = object.base64content.decodeBase64()
def file = new java.io.File("output.pdf")
FileOutputStream fos = new java.io.FileOutputStream(file)
fos.write( content_decoded )
fos.flush()
fos.close()
感谢@letimome,如果我使用上面的解决方案,使用下面的代码
String content\u decoded=actualbase64.decodeBase64()def file=new java.io.file('D:/Apps/Intellidoc\u LVL/base64Decoded2.pdf')log.info file log.info content\u decoded FileOutputStream fos=new java.io.FileOutputStream(file)fos.write(content\u decode)fos.flush()fos.close()
但我发现以下错误启动失败:Script104.groovy:73:变量名无效。必须以字母开头,但为:。在[73:73]@第73行第73列。lidoc_LVL/base64Decoded2.pdf')^
,你知道为什么它无法解释文件路径吗?这里的解决方案对我有效,那么这个问题是重复的。由于原始问题已解决此问题,我建议您删除此问题。