Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning image from remote server play framework java

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?

like image 963
Swapnil B. Avatar asked Feb 17 '26 06:02

Swapnil B.


1 Answers

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"));
    }

}
like image 67
Andriy Kuba Avatar answered Feb 19 '26 19:02

Andriy Kuba