Reputation: 359
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
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
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