user3687679
user3687679

Reputation: 359

Send image as API response

I need to send images as API response.I created a response but i still cant not send the image.I am using play framework with java.

Http.Response response = new Http.Response();
        response.setContentType("image/jpeg");

Thanks.

Upvotes: 1

Views: 2608

Answers (2)

Mon Calamari
Mon Calamari

Reputation: 4463

Previous answer is correct, however if image is large, I would suggest using chunks and streams:

public Result send() throws FileNotFoundException {
    FileInputStream fis = new FileInputStream(new File("some_path/test.jpeg"));
    response().setContentType("image/jpeg");
    return ok(fis);
}

Upvotes: 0

Santiago Ferrer Deheza
Santiago Ferrer Deheza

Reputation: 312

Do not really know what version of play are you using but this should work for 2.X

public static Result returnImage(){     
    return ok(new File("public/img/1.jpg")).as("image/jpg");  
}

Here you can see that ok() can receive a File as a parameter. To check all the options you can go to Play Framework JavaDocs.

Hope it helps!

Upvotes: 1

Related Questions