我想使用RestAssured从网站下载图片。我的代码:
Response response = given()
.log().all()
.cookie(cookie)
.get(format(image_endpoint, "46581657"));
端点以“application / octet-stream”类型返回状态代码200和图像。如何将其保存为jpg文件?
我试过了:
String bin = getImageResponse()
.prettyPrint();
saveFile("foto.jpg", bin); //this method only save string to file
但我无法打开jpg文件。我怎么能保存它?
答案 0 :(得分:0)
MIME类型application / octet-stream由Base64中编码的字节组成。使用Base64类进行解码。
private void saveFile(String path, String octetStream) throws IOException{
byte[] bytes = Base64.getDecoder().decode(octetStream);
try(FileOutputStream fos = new FileOutputStream(path)){
fos.write(bytes);
}
}
我对RestAssured一无所知,所以也许框架提供了任何方式。