Groovy - 如何解码 base 64 字符串并将其保存到本地目录中的 pdf/doc

Groovy - how to Decode a base 64 string and save it to a pdf/doc in local directory

问题

我需要帮助解码 base 64 字符串并使用 groovy 将其保存到本地目录中的 pdf/doc 这个脚本应该在 SOAP 中工作 UI base64字符串长度为52854个字符

我试过以下

File f = new File("c:\document1.doc")
FileOutputStream out = null 
byte[] b1 = Base64.decodeBase64(base64doccontent);
out = new FileOutputStream(f)
try {
    out.write(b1)
} finally {
    out.close()
}

但是 - 它给了我以下错误

没有方法签名:static org.apache.commons.codec.binary.Base64.decodeBase64() 适用于参数类型:(java.lang.String) 值:[base64stringlong] 可能的解决方案:decodeBase64([B), encodeBase64 ([B), encodeBase64([B, 布尔值)

假设 base64 编码的文本来自文件,soapUI 的最小示例是:

import com.itextpdf.text.*
import com.itextpdf.text.pdf.PdfWriter;

String encodedContents = new File('/path/to/file/base64Encoded.txt')
    .getText('UTF-8')
byte[] b1 = encodedContents.decodeBase64();

// Save as a text file
new File('/path/to/file/base64Decoded.txt').withOutputStream {
        it.write b1
} 

// Or, save as a PDF
def document = new Document()
PdfWriter.getInstance(document, 
    new FileOutputStream('/path/to/file/base64Decoded.pdf'))

document.open()
document.add(new Paragraph(new String(b1)))
document.close()

File.withOutputStream方法将确保流在关闭时关闭returns。

或者,为了将字节数组转换为 PDF,我使用了 iText。我将 itextpdf-5.5.13.jar 放入 soapUI 的 bin/ext 目录并重新启动,然后它可用于 Groovy.