Reputation: 759
I tried to return the image as shown below:
return ok(new File("http://example.com/dpa/client_name/images/client_log.jpg"));
but the method in the controller couldn't fetch the image from the remote server and threw a image not found exception.
How do I retrieve an image from the remote server and return as a response using java play framework?
Upvotes: 3
Views: 1841
Reputation: 8263
Simply use WS API
package controllers;
import play.libs.ws.WSClient;
import play.mvc.Controller;
import play.mvc.Result;
import java.util.concurrent.CompletionStage;
import javax.inject.Inject;
public class HomeController extends Controller {
@Inject WSClient ws;
public CompletionStage<Result> index() {
return ws
.url("http://www.maine-coon-cat-nation.com/image-files/orange-maine-coon-cat.jpg")
.get()
.thenApply(file -> ok(file.getBodyAsStream()).as("image/jpeg"));
}
}
Upvotes: 9