有一个GET API REST调用请求,on executed提供了一个zip文件,下面是响应的头文件
content-disposition →attachment;filename="results.zip"
content-type →text/plain; charset=UTF-8
在Postman上,我们可以执行Send and Download
,并可以保存生成的压缩文件
我使用RestAssured
测试REST API调用。
有人能告诉我如何从API调用中检索得到的zip文件吗?
发布于 2017-11-06 11:44:14
更新:我从Rest Assured找到了一个解决方案,我们可以获得InputStream或字节数组形式的响应,并进一步写入ZipFile。
作为InputStream:
// Get the response as Input Stream
InputStream is = result.getBody().asInputStream();
OutputStream stream = new FileOutputStream("D:\\Test.zip");
int read = 0;
byte[] bytes = new byte[1024];
while ((read = is.read(bytes)) != -1) {
stream.write(bytes, 0, read);
}
System.out.println("Zip file captured successfully");
作为ByteArray:-
//获取ByteArray形式的响应
byte[] arraybyteResponse = result.getBody().asByteArray();
ByteBuffer buffer = ByteBuffer.wrap(arraybyteResponse);
OutputStream os = new FileOutputStream("D:\\Test.zip");
WritableByteChannel channel = Channels.newChannel(os);
channel.write(buffer);
System.out.println("Zip file captured successfully");
https://stackoverflow.com/questions/47032419
复制相似问题