Created
August 16, 2017 20:53
-
-
Save m-x-k/94adcd1084a17fa83adf96f5b99630ff to your computer and use it in GitHub Desktop.
Spring MVC return output stream as response using lambda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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