首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Grails呈现HttpInputStream为pdf

Grails呈现HttpInputStream为pdf
EN

Stack Overflow用户
提问于 2014-03-12 15:26:37
回答 3查看 1K关注 0票数 0

通过下面的代码,我得到了这样糟糕的输出:

%PDF-4% <>stream 4 0 obj x�3P0T�5T0P034�ɹ\�\n\�f��f !)\�!\�\�%@(Dɹ�i�.�h�@]z PF.�endobj endobj 6 0 obj <>/ProcSet /PDF /Text /ImageB /ImageC /ImageI>>/MediaBox0 0 612 792/ 90>> endobj 7 0 obj <>stream x/PDF}许;d“7,后继人a H!!HDfLh 2 A�%Dhj�:b�D����T�Z�U$��>��pEѶ�����$���=k������DA��g�E�}2^�!�9'�Zp���_R��/��=��Y�W����ߝ7��P�S#J*E���7)�����>�������f����͟9�2�AEE E"8K���������3t�g�?cɂ������!�o���y����Z���;R�|J!2>>f�:������O����6z@̡h=.�@�i5�)�Jѯ�t�“‘ME���ǁ��D��L�h���^@ڳ�KI”�����J��k%�P�q4�։3�����W�@:�.����4�1n0i��G�EQ��������ƛ�9n��mqC“E1Rf:�CD- Hy-��4�ߡ�T1ڠ�zA���^G��T�R�4B��*���l�@{�ߡ�;��1���:���[��v�i���"���q���Rw:�i��~�����bp�.u����O���

而不是pdf。以下是代码:

代码语言:javascript
复制
URL url = new URL(urlStr)
URLConnection connection = url.openConnection();
response.contentType = 'application/pdf'
response.setHeader("Content-Disposition","Attachment; filename=gdoc.pdf")
def bytes = connection.getInputStream().getBytes()
response.getOutputStream().write(bytes)
response.outputStream.flush()
response.outputStream.close()

/*
  //writing to a pdf file works perfectly
  def outputStream =  new FileOutputStream(new File("gdoc/abc.pdf"));
  outputStream.write(bytes)
  outputStream.close()
*/

如何在浏览器中获得实际的pdf输出。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-03-12 16:47:34

你是在试着用iframe来回应吗?如果是,首先尝试在一个新窗口中直接打开输出,并查看它是否打开。

如果它打开,尝试将您的控制器映射到类似于abc.pdf的东西,以便您指向的url变成/ the /abc.pdf。

票数 1
EN

Stack Overflow用户

发布于 2014-03-12 15:53:13

这假设您在控制器中,控制器方法末尾的返回null确保Grails不尝试并呈现任何内容。此外,使用Apache复制内容将防止您在将整个文件发送到响应之前将其下载到内存中。这是从生产代码中修改的,我在那里做了一些类似的事情。Grails版本1.3.7和2.0.4。

代码语言:javascript
复制
URL url = new URL(urlStr)

response.setContentType("application/pdf")
response.setHeader("Content-disposition", "attachment;filename=gdoc.pdf")

// this will actually buffer/stream the file in 8k chunks instead of reading the entire file into memory.
org.apache.commons.io.IOUtils.copy(url.openStream(), response.outputStream)
response.outputStream.flush()
response.outputStream.close()

// make sure Grails doesn't render anything for the request.
return null
票数 1
EN

Stack Overflow用户

发布于 2015-02-20 13:17:49

我解决了执行以下代码的问题:

主计长:

代码语言:javascript
复制
    def bytes = SendHttpService.executeGetBinary("http://app.com",  
                                                  "/pdf/", params)
    response.setHeader("Expires", "0");
    response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
    response.setHeader("Pragma", "public");
    response.setHeader("Content-Disposition", "inline; filename=\"relatorioGerencial.pdf\"");
    response.setContentType("application/pdf");
    response.setContentLength(bytes.length);
    ServletOutputStream ouputStream = response.getOutputStream();
    ouputStream.write(bytes,0,bytes.length);
    ouputStream.flush();
    ouputStream.close();

在这里,像第一个例子一样发送返回流的请求,注意contentType是二进制的

代码语言:javascript
复制
    def executePostBinary = { String httpUrl, String dataBody, String path, query = null, method = Method.POST->

    def http = new HTTPBuilder()
    try{
        http.request( httpUrl , method , ContentType.BINARY ) { req ->

            uri.path = path
            uri.query = trataQuery(query)
            headers.'User-Agent' = "Mozilla/5.0 Firefox/3.0.4"
            headers.Accept = ContentType.BINARY
            if(method==groovyx.net.http.Method.POST && dataBody!=null){
                body = dataBody
            }

            response.success = { resp, inputStream  ->
                inputStream.bytes

            }

            response.'404' = {
                'Not found'
            }
        }
    }catch(HttpResponseException e){
        println "Error "+e
        println "Error Code "+e.statusCode
        return false
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22356183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档