Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Grails将HttpInputStream呈现为pdf格式_Java_Jakarta Ee_Pdf_Grails_Inputstream - Fatal编程技术网

Java Grails将HttpInputStream呈现为pdf格式

Java Grails将HttpInputStream呈现为pdf格式,java,jakarta-ee,pdf,grails,inputstream,Java,Jakarta Ee,Pdf,Grails,Inputstream,通过以下代码,我得到了如下错误输出: %PDF-1.4%���� 4 0 obj流x�30吨�5T0P034�ɹ\�\N\�F��f!)\�!\�\� %@(Dɹ�我� .� H�@]Z��PF��� endstream endobj 6 0 obj/ProcSet[/PDF/Text/ImageB/ImageC/ImageI]>/MediaBox[0 0 612 792]/Rotate 90>>endobj 7 0 obj stream x��} 徐������;d“7!�托尔���A.�H

通过以下代码,我得到了如下错误输出:

%PDF-1.4%���� 4 0 obj流x�30吨�5T0P034�ɹ\�\N\�F��f!)\�!\�\� %@(Dɹ�我� .� H�@]Z��PF��� endstream endobj 6 0 obj/ProcSet[/PDF/Text/ImageB/ImageC/ImageI]>/MediaBox[0 0 612 792]/Rotate 90>>endobj 7 0 obj stream x��} 徐������;d“7!�托尔���A.�H.!HDfLh� 2.�AEE“8K�� A.�%Dhj�:B�D����T�Z�U$��>��佩Ѷ�����$���=K������DA��G�E�}[2^�!�9'�Zp���_R��/��=��Y�W����ߝ]7��P�S#J*E���7)�����>�������F����͟9�����3t�G�?cɂ������!�o���Y����Z���;�R*�渗R�|J2> >f�:������O����6z@̡h=。�@�i5�)�Jѯ�T�“‘我’���ǁ��D��L�H���^@ڳ� �KI“�����J��k%�P�第四季度�։3�����W�@:�.����4.�1n0我��G�情商��������ƛ�9n�[�mqC�CD-Hy-� �4]�ߡ�t1ڠ�zA���^G��T�R�4B��*���L�@{E1Rf;��1.���:���[��v�我���"���Q���Rw:�我��~�����英国石油公司�.U����O���

而不是pdf。以下是代码:

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输出。

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

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

您是否在iframe中尝试响应?如果是,请首先尝试直接在新窗口中打开输出,然后查看它是否打开


如果打开,请尝试将控制器映射到类似abc.pdf的内容,以便您指向的url变成/something/abc.pdf。

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

控制器:

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


您在写入响应后是否尝试过response.flush()和response.close()?我尝试过您的建议,请查看已编辑的代码。但仍然没有成功,它显示了相同的错误输出。此输出看起来像PDF,btwI无法执行
(新URL(URL))。openStream()
因为我必须在url中传递一个头。所以我用
connection.getInputStream()
替换了这一部分,其中connection是
URLConnection
的实例。但是这也不起作用,呈现相同的垃圾输出对不起,我没有改变我原来的代码。我只是将它编辑为url.openStream()。可能与
FileOutputStream
HttpInputStream
有关。您使用的是前者,而我使用的是后者。无论如何,谢谢:)我没有在任何地方使用文件输出流,但好的。哦,sry,对评论感到困惑。
    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();
    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
    }
}