通过下面的代码,我得到了这样糟糕的输出:
%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�1n0i��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。以下是代码:
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输出。
发布于 2014-03-12 16:47:34
你是在试着用iframe来回应吗?如果是,首先尝试在一个新窗口中直接打开输出,并查看它是否打开。
如果它打开,尝试将您的控制器映射到类似于abc.pdf的东西,以便您指向的url变成/ the /abc.pdf。
发布于 2014-03-12 15:53:13
这假设您在控制器中,控制器方法末尾的返回null确保Grails不尝试并呈现任何内容。此外,使用Apache复制内容将防止您在将整个文件发送到响应之前将其下载到内存中。这是从生产代码中修改的,我在那里做了一些类似的事情。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发布于 2015-02-20 13:17:49
我解决了执行以下代码的问题:
主计长:
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是二进制的
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
}
}https://stackoverflow.com/questions/22356183
复制相似问题