1

In a Grails 2.1.1 controller I'm attempting to stream a video (mp4) file to the browser.

The following code works. However, since I'm not specifying a content type the browser will save the file directly.

    OutputStream os = response.outputStream
    ByteArrayInputStream is = new ByteArrayInputStream(videoInstance.data)

    response.contentLength = videoInstance.data.length
    os << is

If I try to change it to specify a content type...

    OutputStream os = response.outputStream
    ByteArrayInputStream is = new ByteArrayInputStream(videoInstance.data)

    response.contentType = videoInstance.contentType
    response.contentLength = videoInstance.data.length
    os << is

...suddenly I get exceptions.

Software caused connection abort: socket write error. Stacktrace follows:
Message: Software caused connection abort: socket write error

and later

ERROR errors.GrailsExceptionResolver  - IllegalStateException occurred when processing    request: getOutputStream() has already been called for this response.

I've tried adding both a .os.flush() and os.close() to no avail. Any ideas what could cause this?

user577905
  • 173
  • 2
  • 8
  • have you tried to change the order of your `response` calls and to write the `InputStream` directly to the response's output stream, so you won't have to instantiate it at first? like `response.outputStream << is`? – herom Feb 14 '13 at 08:09
  • Yes, I have. That's what I was doing initially. Same result. – user577905 Feb 14 '13 at 12:40
  • Well, you could try the approach from this question, even if it's rather old (if you haven't done yet) http://stackoverflow.com/questions/8279192/render-video-content-from-a-grails-controller - setting content type by calling `.addHeader()` method may change something, hopefully ... – herom Feb 14 '13 at 13:09
  • The problem ended up being that the video wasn't encoded properly to display in Chrome's built-in video player. Oops. – user577905 Feb 17 '13 at 23:44
  • ah, ok. thanks for sharing your experience, so that this question won't hang without any further information on this topic. glad you found the solution for your problem :) – herom Feb 18 '13 at 10:14

0 Answers0