Skip to content

Instantly share code, notes, and snippets.

@m-x-k
Created August 16, 2017 20:53
Show Gist options
  • Save m-x-k/94adcd1084a17fa83adf96f5b99630ff to your computer and use it in GitHub Desktop.
Save m-x-k/94adcd1084a17fa83adf96f5b99630ff to your computer and use it in GitHub Desktop.
Spring MVC return output stream as response using lambda
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
@Controller
public class StreamControllerExample {
@RequestMapping("/")
public StreamingResponseBody index() throws IOException {
URL url = new URL("http://SOMEPLACE/case.jpg");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
return outputStream -> IOUtils.copy(connection.getInputStream(), outputStream);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment