1

我正在通过 Grails 下载文件,并记录该文件已被该用户下载的事实,代码如下:

class MyController {

    private void recordTrackDownload() {
        def d = new Download(session.user, "/path/to/file")
        d.save()
    }

    def download = {
        def f = new File("/path/to/file")
        recordTrackDownload()
        response.contentType = "mime/type"
        response.outputStream << f.newInputStream()
        response.outputStream.flush()
    }
}

而且我看到每次点击都会出现几条下载记录。我怀疑这与 Firefox 记录的几个请求和我在 Live HTTP Headers 中看到的“Transfer-Encoding: chunked”标头有关。(这种行为在 Chromium 中也很明显)

我还看到终端中记录了一个错误,这可能是相关的,可能是 Grails 中的一个错误,或者只是一个已记录并被忽略的错误,但它似乎并没有阻止文件实际下载:

2010-01-14 18:46:16,623 [http-8080-4] ERROR errors.GrailsExceptionResolver  - Executing action [download] of controller [com.tunited.music.DownloadTrackController]  caused exception: ClientAbortException:  java.net.SocketException: Connection reset
org.codehaus.groovy.grails.web.servlet.mvc.exceptions.ControllerExecutionException: Executing action [download] of controller [com.tunited.music.DownloadTrackController]  caused exception: ClientAbortException:  j
ava.net.SocketException: Connection reset
        at java.lang.Thread.run(Thread.java:636)
Caused by: org.codehaus.groovy.runtime.InvokerInvocationException: ClientAbortException:  java.net.SocketException: Connection reset
        ... 1 more
Caused by: ClientAbortException:  java.net.SocketException: Connection reset
        at com.tunited.music.DownloadTrackController$_closure2.doCall(DownloadTrackController.groovy:35)
        at com.tunited.music.DownloadTrackController$_closure2.doCall(DownloadTrackController.groovy)
        ... 1 more
Caused by: java.net.SocketException: Connection reset
        at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
        at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
        ... 3 more
2010-01-14 18:46:17,091 [http-8080-4] ERROR view.GroovyPageView  - Error processing GroovyPageView: getOutputStream() has already been called for this response
java.lang.IllegalStateException: getOutputStream() has already been called for this response
        at home_ed_dev_nomad_tunited_grails_app_views_error_gsp.run(home_ed_dev_nomad_tunited_grails_app_views_error_gsp:19)
        at java.lang.Thread.run(Thread.java:636)
2010-01-14 18:46:17,092 [http-8080-4] ERROR errors.GrailsExceptionResolver  - getOutputStream() has already been called for this response
java.lang.IllegalStateException: getOutputStream() has already been called for this response
        at java.lang.Thread.run(Thread.java:636)
2010-01-14 18:46:17,100 [http-8080-4] ERROR view.GroovyPageView  - Error processing GroovyPageView: getOutputStream() has already been called for this response
java.lang.IllegalStateException: getOutputStream() has already been called for this response
        at home_ed_dev_nomad_tunited_grails_app_views_error_gsp.run(home_ed_dev_nomad_tunited_grails_app_views_error_gsp:19)
        at java.lang.Thread.run(Thread.java:636)
2010-01-14 18:46:17,103 [http-8080-4] ERROR [/].[grails]  - Servlet.service() for servlet grails threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
        at org.apache.catalina.connector.Response.getWriter(Response.java:610)
        at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
        at org.codehaus.groovy.grails.web.sitemesh.GrailsPageResponseWrapper$5.activateDestination(GrailsPageResponseWrapper.java:141)
        at org.codehaus.groovy.grails.web.sitemesh.GrailsRoutablePrintWriter.getDestination(GrailsRoutablePrintWriter.java:41)
        at org.codehaus.groovy.grails.web.sitemesh.GrailsRoutablePrintWriter.flush(GrailsRoutablePrintWriter.java:159)
        at org.codehaus.groovy.grails.web.util.GrailsPrintWriter.flush(GrailsPrintWriter.java:98)
...
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
        at java.lang.Thread.run(Thread.java:636)
2010-01-14 18:46:17,106 [http-8080-4] ERROR [/].[default]  - Servlet.service() for servlet default threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
        at org.apache.catalina.connector.Response.getWriter(Response.java:610)
        at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
        at org.codehaus.groovy.grails.web.sitemesh.GrailsPageResponseWrapper$5.activateDestination(GrailsPageResponseWrapper.java:141)
        at org.codehaus.groovy.grails.web.sitemesh.GrailsRoutablePrintWriter.getDestination(GrailsRoutablePrintWriter.java:41)
        at org.codehaus.groovy.grails.web.sitemesh.GrailsRoutablePrintWriter.flush(GrailsRoutablePrintWriter.java:159)
        at org.codehaus.groovy.grails.web.util.GrailsPrintWriter.flush(GrailsPrintWriter.java:98)
...
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
        at java.lang.Thread.run(Thread.java:636)

理想情况下,我希望我的控制器代码只执行一次,并下载分块文件。我可以通过几个动作来做到这一点,第一个动作记录下载,然后重定向到第二个下载文件。但是设置另一个重定向并且必须使用诸如 flash 范围之类的东西来保护第二个 url,这似乎是一个糟糕的解决方案。似乎我不知道 HTTP 协议或 Grails 是如何工作的,但有一种简单的方法可以解决这个问题。

有任何想法吗 ?

4

2 回答 2

1

真正简单的解决方法是不要对编码进行分块。如果您设置内容长度,则不应有分块。

就像是:

response.contentType = "mime/type"
response.contentLength = f.length()
于 2010-01-15T11:59:59.113 回答
0

我猜您上面的错误是由用户取消下载引起的。这将导致错误,并且由于某些数据已在响应中发送,因此您看到的异常来自 grails 试图将错误消息写入响应。

于 2012-07-17T21:42:51.563 回答