2013-06-20 40 views
2

我正在使用Grails插件rest = 0.7來使用Rest Web服務。無法通過在Grails中使用REST web服務來下載pdf文件

當服務的響應是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響應的情況。

請幫我解決這個問題,我不知道我使用的方法是有效的,如果從web服務的文件響應。

回答

0

嘗試增加

response.setContentType("text/pdf"); 

response = httpClient.execute(httpPost);