我正在通过 SoapUI
测试 wsdl
文件,并希望将 pdf 文件的 byte[]
发送给客户端。客户端收到byte[]
,但是创建的pdf文件是空白的。以下是我所做的:
SoapUI 响应脚本:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
byte[] pdfBytes = new File(groovyUtils.projectPath + "//file1.pdf").text.getBytes( 'UTF-8' )
requestContext.pdfBytes = pdfBytes.encodeBase64()
Java 代码:
byte[] bytes = getFileFromServer();
File f = new File(filePath);
try {
FileOutputStream fileOuputStream = new FileOutputStream(f);
fileOuputStream.write(bytes);
fileOuputStream.close();
} catch (FileNotFoundException e) {
LOG.error("FileNotFoundException", e);
} catch (IOException e) {
LOG.error("IOException", e);
}
通过使用解码,创建的文件被损坏:
bytes = Base64.decodeBase64(bytes);
有什么想法吗?
最佳答案
这就是问题 - 或者至少问题:
new File(groovyUtils.projectPath + "//file1.pdf").text
您正在加载一个二进制文件,就好像它是文本一样。它不是。不要将其作为文本读取,然后将其转换为字节 - 使用
byte[] pdfBytes = new File(groovyUtils.projectPath + "//file1.pdf").bytes
关于java - 如何通过soapUI发送pdf文件的数组字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23740998/