【问题标题】:Unable to download pdf file by consuming REST webservice in Grails无法通过在 Grails 中使用 REST Web 服务来下载 pdf 文件
【发布时间】:2013-06-20 06:09:56
【问题描述】:

我正在使用 Grails Plugin rest=0.7 来使用 Rest Webservice。

当服务的响应是 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响应的情况。

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

【问题讨论】:

    标签: java web-services rest grails post


    【解决方案1】:

    尝试添加

    response.setContentType("text/pdf");
    

    之前

    response = httpClient.execute(httpPost);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-17
      • 1970-01-01
      • 2016-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多