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

Groovy-如何解码base 64字符串并将其保存到本地目录中的pdf/doc,groovy,soapui,Groovy,Soapui,问题 我需要有关解码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

问题

我需要有关解码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
方法将确保在关闭返回时关闭流


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

假设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
方法将确保在关闭返回时关闭流


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

如果
base64doccontent
是一个字符串,请尝试:
base64doccontent.decodeBase64()
。如果
base64doccontent
是字符串,请尝试:
base64doccontent.decodeBase64()
。非常感谢@craigcaulfield,很抱歉回复太晚。我使用itextpdf-5.5.13.jar尝试了您的方法,但在尝试运行此
时遇到以下错误:java.lang.String.getText()适用于参数类型:(java.lang.String)值:[UTF-8]可能的解决方案:getAt(java.lang.String)、getAt(java.util.Collection)、getAt(groovy.lang.IntRange)、getAt(int)、getAt(groovy.lang.Range)、getAt(groovy.lang.EmptyRange)
我是否需要导入任何其他库?这是根据您之前的建议,我用于将base64字符串保存为pdf的代码
def encodedContent=actualbase64.getText('UTF-8')//actualbase64是base64编码字符串字节[]b1=encodedContents.decodeBase64()def document=new document()PdfWriter.getInstance(文档,新文件输出流('D:/path/to/file/base64Decoded.pdf'))document.open()文档.add(新段落(新字符串(b1)))文档.close()
上面的示例假设您正在将文件中的编码内容读入字符串。但是,您的
actualbase64
已经是一个字符串,因此不需要行
def encodedContent=actualbase64.getText('UTF-8')
。只需使用
byte[]b1=actualbase64.decodeBase64()
。这很有效-再次感谢@craigcaulfield。但现在pdf内容乱七八糟,如下图所示,我如何将其显示为完全格式化的文本。如果我使用转换器手动转换,这很有效
%pdf-1.5 5 0 obj此方法适用于我通常在soapUI中遇到的简单文本文件,但更丰富的内容可能需要更多按摩。因为这些评论中没有太多空间,所以最好解决这个问题,并用您正在使用的文档的示例提出一个单独的问题。非常感谢@craigcaulfield,很抱歉回复太晚。我使用itextpdf-5.5.13.jar尝试了您的方法,但在尝试运行此操作时,我遇到了以下错误
没有方法签名:java.lang.String.getText()适用于参数类型:(java.lang.String)值:[UTF-8]可能的解决方案:getAt(java.lang.String)、getAt(java.util.Collection)、getAt(groovy.lang.IntRange)、getAt(int)、getAt(groovy.lang.Range)、getAt(groovy.lang.EmptyRange)
我是否需要导入任何其他库?这是根据您之前的建议,我用于将base64字符串保存为pdf的代码
def encodedContent=actualbase64.getText('UTF-8')//actualbase64是base64编码字符串字节[]b1=encodedContents.decodeBase64()def document=new document()PdfWriter.getInstance(文档,新文件输出流('D:/path/to/file/base64Decoded.pdf'))document.open()文档.add(新段落(新字符串(b1)))文档.close()
上面的示例假设您正在将文件中的编码内容读入字符串。但是,您的
actualbase64
已经是一个字符串,因此不需要行
def encodedContent=actualbase64.getText('UTF-8')
。只需使用
byte[]b1=actualbase64.decodeBase64()
。这很有效-再次感谢@craigcaulfield。但现在pdf内容乱七八糟,如下图所示,我如何将其显示为完全格式化的文本。如果我使用转换器手动转换,这很有效
%pdf-1.5 5 0 obj此方法适用于我通常在soapUI中遇到的简单文本文件,但更丰富的内容可能需要更多按摩。因为这些评论没有太多的空间,所以最好解决这个问题,并用您正在使用的文档的示例单独提出一个问题。