Groovy Grails, How do you stream or buffer a large file in a Controller's response?

Posted by Julian Noye on Stack Overflow See other posts from Stack Overflow or by Julian Noye
Published on 2010-05-13T04:52:48Z Indexed on 2010/05/13 7:04 UTC
Read the original article Hit count: 286

Hi Guys

I have a controller that makes a connection to a url to retrieve a csv file.

I am able to send the file in the response using the following code, this works fine.

    def fileURL = "www.mysite.com/input.csv"
    def thisUrl = new URL(fileURL);
    def connection = thisUrl.openConnection();
    def output = connection.content.text;

    response.setHeader "Content-disposition", "attachment;
    filename=${'output.csv'}"
    response.contentType = 'text/csv'
    response.outputStream << output
    response.outputStream.flush()

However I don't think this method is inappropriate for a large file, as the whole file is loaded into the controllers memory.

I want to be able to read the file chunk by chunk and write the file to the response chunk by chunk.

Any ideas?

© Stack Overflow or respective owner

Related posts about grails

Related posts about grails-controller