我正在通过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)
理想情况下,我的控制器代码只能执行一次,而且要下载的分块文件。我可以通过多个操作来完成此操作,第一个操作记录下载,然后重定向到下载文件的第二个操作。但是使用类似闪存范围之类的东西,将另一个重定向放在路上并且必须保护第二个URL,这似乎是一个糟糕的解决方案。似乎只有我不知道HTTP协议或Grails如何工作的东西,并且有一种简单的方法可以解决这个问题。
有什么想法吗?
答案 0 :(得分:0)
真正简单的解决方法不是对编码进行分块。如果设置内容长度,则不应该有分块。
类似的东西:
response.contentType = "mime/type"
response.contentLength = f.length()
答案 1 :(得分:0)
我猜你上面的错误是由用户取消下载引起的。这会导致错误,并且由于某些数据已在响应中发送,您看到的异常是来自尝试将错误消息写入响应的grails。