Groovy Grails,如何在控制器';谁的反应?

Groovy Grails,如何在控制器';谁的反应?,grails,response,grails-controller,Grails,Response,Grails Controller,我有一个控制器,可以连接到url以检索csv文件 我能够使用以下代码在响应中发送文件,这很好 def fileURL = "www.mysite.com/input.csv" def thisUrl = new URL(fileURL); def connection = thisUrl.openConnection(); def output = connection.content.text; response.setHeader "Content-

我有一个控制器,可以连接到url以检索csv文件

我能够使用以下代码在响应中发送文件,这很好

    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()
def fileURL=“www.mysite.com/input.csv”
def thisUrl=新URL(fileURL);
def connection=thisUrl.openConnection();
def输出=connection.content.text;
response.setHeader“内容处置”、“附件”;
filename=${'output.csv'}”
response.contentType='text/csv'

Groovy可以使用
好的解决方案直接获取inputStream,但也要记住关闭inputStream。安全的替代方法是使用connection.withInputStream{…}
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()