在Grails

时间:2017-06-14 14:58:48

标签: pdf grails groovy

我正在尝试将pdf文档从我的控制器传递给我的视图

我使用的渲染方法如下:

        render (file: new File (result), fileName:"test.pdf", contentType:"application/pdf" )

结果是:“file:/// D:/TempPartage/test.pdf” 问题是我得到了当前的例外:

  

无法确定file:test.pdf的内容类型。 Stacktrace如下:   org.codehaus.groovy.grails.web.servlet.mvc.exceptions.ControllerExecutionException:无法确定文件的内容类型:test.pdf       at fr.mma.usage.EditionintercalaireController $$ EQMXBXRn.copierPDF(EditionintercalaireController.groovy:154)       at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)       at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)       at fr.mma.use.logging.filter.HttpLoggingFilter.doFilter(HttpLoggingFilter.java:69)       at fr.mma.souse.authentication.AnnuaireCredentialCheckerFilter.doFilter(AnnuaireCredentialCheckerFilter.groovy:84)       at fr.mma.souse.security.mock.MockAuthenticationFilter.doFilter(MockAuthenticationFilter.groovy:60)       在org.togglz.servlet.TogglzFilter.doFilter(TogglzFilter.java:100)       at fr.mma.use.navigation.NexturlValidationFilter.doFilter(NexturlValidationFilter.groovy:52)       at fr.mma.use.requestid.RequestIdFilter.doFilter(RequestIdFilter.groovy:38)       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)       在java.lang.Thread.run(Thread.java:722)

我做错了什么? 感谢帮助

1 个答案:

答案 0 :(得分:1)

"file:///D:/TempPartage/test.pdf"看起来像URL而不是文件路径

两种变体:

render (
    file: new URL(result).openStream(), //file could be stream
    fileName:"test.pdf", 
    contentType:"application/pdf" 
)

render (
    file: new File( new URL(result).toURI().getPath() ), 
    fileName:"test.pdf", 
    contentType:"application/pdf" 
)