gpt4 book ai didi

java - 无法通过在 Grails 中使用 REST Web 服务来下载 pdf 文件

转载 作者:行者123 更新时间:2023-11-29 09:01:26 25 4
gpt4 key购买 nike

我正在使用 Grails 插件 rest=0.7 来使用 Rest Web 服务。

当来自服务的响应是 xml 时,一切正常,但如果响应是 pdf 等文件类型,它必须在发送请求时开始下载,但下载根本没有开始。

以下代码在 grails 服务中实现。

    String httpUrl = 'http://abc.com/myService'
String data = '<methodcall protocol="2" method="avalidmethodname"><cmdid/><data><project_id>1</project_id><user_id>2</user_id><operation>ABC</operation><filter><status_type_id>1</status_type_id><scope_bits>00</scope_bits></filter></data></methodcall>'
String respText = ''
HttpClient httpClient = new DefaultHttpClient()
HttpResponse response
try {
HttpPost httpPost = new HttpPost(httpUrl)
httpPost.setHeader("Content-Type", "text/xml")
HttpEntity reqEntity = new StringEntity(data, "UTF-8")
reqEntity.setContentType("text/xml")
reqEntity.setChunked(true)
httpPost.setEntity(reqEntity)
response = httpClient.execute(httpPost)
// HttpEntity resEntity = response.getEntity()
// respText = resEntity.getContent().text
}
finally {
httpClient.getConnectionManager().shutdown()
}
return response
// return respText

代码中的注释行是针对 xml 响应的情况。

请帮我解决这个问题,我不确定我使用的方法在来自网络服务的文件响应的情况下是否有效。

最佳答案

尝试添加

response.setContentType("text/pdf");

之前

response = httpClient.execute(httpPost);

关于java - 无法通过在 Grails 中使用 REST Web 服务来下载 pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17206438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com