Groovy-如何解码基本64字符串并将其保存到本地目录中的PDF/DOC



问题

我需要使用Groovy在本地目录中解码基本64字符串并将其保存到pdf/doc的帮助该脚本应该在肥皂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.base.base64.decodebase64((适用于参数类型:(,encodebase64([b(,encodebase64([b,boolean(

假设基本64编码的文本来自文件,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方法将确保闭合返回时关闭流。

或,要将字节数组转换为pdf,我使用了itext。我在Soapui的bin/ext目录中丢弃了itextpdf-5.5.13.jar,然后重新启动,然后可用于Groovy。

最新更新