views:

357

answers:

3

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?

A: 

In case you have not found it, this link talks about "High performance: exporting data to Excel in CSV format with Grails"

http://tijhuis.wordpress.com/2008/08/21/high-performance-exporting-data-to-excel-in-csv-format-with-grails/

john
+1  A: 

Groovy OutputStreams can take InputStreams directly with the << operator. The OutputStream will pull the data automatically with an appropriately sized buffer.

The following should efficiently copy the data, even if the CSV is quite large.

def fileURL = "www.mysite.com/input.csv"
def thisUrl = new URL(fileURL);
def connection = thisUrl.openConnection();
def cvsInputStream = connection.inputStream

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

Thanks Guys

"ataylor" your solution works very well, much appreciated. :)

Julian