我正在通过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 header 中看到的“Transfer-Encoding:chunked” header 有关。 (这种行为在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)
理想情况下,我希望 Controller 代码仅执行一次,然后下载分块文件。我可以通过执行几个操作来做到这一点,第一个记录下载,然后重定向到第二个下载文件。但是,使用Flash作用域之类的方式放置另一个重定向并必须保护第二个URL似乎是一个糟糕的解决方案。似乎我对HTTP协议(protocol)或Grails的工作方式一无所知,并且有一种简单的方法可以解决此问题。
有任何想法吗 ?
最佳答案
真正简单的解决方法是不对编码进行分块处理。如果设置内容长度,则不应有任何分块。
就像是:
response.contentType = "mime/type"
response.contentLength = f.length()
关于http - 使用分块传输编码时可以处理多个请求,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2066440/