Groovy - 如何解码base 64字符串并将其保存到本地目录中的pdf / doc [英] Groovy - how to Decode a base 64 string and save it to a pdf/doc in local directory
问题描述
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)values:[base64stringlong]可能的解决方案:decodeBase64( [B],encodeBase64([B],encodeBase64([B,boolean])
假设base64编码文本来自一个文件,soapUI的一个最简单的例子是:
import com.itextpdf.text。*
import com。 itextpdf.text.pdf.PdfWriter;
字符串encodedContents =新文件('/ path / to / file / base64Encoded.txt')
.getText('UTF-8')
byte [] b1 = encodedContents .decodeBase64();
//保存为文本文件
新文件('/ path / to / file / base64Decoded.txt')。withOutputStream {
it.write b1
}
//或者另存为PDF
def document = new Document()
PdfWriter.getInstance(document,
new FileOutputStream('/ path / to ())
$ b document.open b $ b
File.withOutputStream
方法将确保流或者,为了将字节数组转换为PDF,我使用了 iText 。我在soapUI的 bin / ext
目录中删除了 itextpdf-5.5.13.jar
并重新启动,然后它可用于Groovy 。
Question
I need help with decoding a base 64 string and saving it to a pdf/doc in local directory using groovy This script should work in SOAP UI The base64 string is 52854 characters long
I have tried the following
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()
}
But - it gives me below error
No signature of method: static org.apache.commons.codec.binary.Base64.decodeBase64() is applicable for argument types: (java.lang.String) values: [base64stringlong] Possible solutions: decodeBase64([B), encodeBase64([B), encodeBase64([B, boolean)
Assuming the base64 encoded text is coming from a file, a minimal example for soapUI would be:
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()
The File.withOutputStream
method will ensure the stream is closed when the closure returns.
Or, to convert the byte array to a PDF, I used iText. I dropped itextpdf-5.5.13.jar
in soapUI's bin/ext
directory and restarted and then it was available for Groovy.
这篇关于Groovy - 如何解码base 64字符串并将其保存到本地目录中的pdf / doc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!