[Java] [RestAssured]如何将应用程序/八位字节流响应保存为jpg图像?

时间:2017-11-23 21:20:22

标签: java save rest-assured octetstring

我想使用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文件。我怎么能保存它?

1 个答案:

答案 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一无所知,所以也许框架提供了任何方式。