Groovy - 如何解码base 64字符串并将其保存到本地目录中的pdf / doc [英] Groovy - how to Decode a base 64 string and save it to a pdf/doc in local directory

查看:1201
本文介绍了Groovy - 如何解码base 64字符串并将其保存到本地目录中的pdf / doc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助解码一个base 64字符串并使用groovy $ b将它保存到本地目录中的pdf / doc中$ b此脚本应该在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)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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆