Stream Data From Controller
From Coder's Log
One view that seems to be missing from Spring is a StreamView, which should allow a controller to return an input stream with a particular content-type to allow for download/generating arbitrary binary content.
public class StreamView implements View {
private String contentType;
private InputStream inputStream;
public StreamView(String contentType, InputStream inputStream) {
this.contentType = contentType;
this.inputStream = inputStream;
}
public String getContentType() {
return contentType;
}
public void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType(getContentType());
OutputStream os=response.getOutputStream();
byte[] buffer=new byte[1024];
int count;
while((count=inputStream.read(buffer))>0)
os.write(buffer,0,count);
os.flush();
inputStream.close();
}
}
To Use simply return from a controller
new ModelAndView(new StreamView("content/type",inputStream));
