2016-09-19 171 views
0

我正在使用球衣客戶端對API進行REST調用,該API將多層響應中的Json和PDF文件作爲響應的第一部分和第二部分返回。Mulipart的Jersey客戶端

final Client client = ClientBuilder.newClient(); 
final WebTarget target = client.target(endPoint); 
final Builder request = target.request().header("Authorization", authKey); 
final Response response = request.get(); 
final String readEntity = response.readEntity(String.class); 

這將返回字節碼格式的PDF文件的字符串響應。 我試圖讀取該實體爲類,然後我得到一個異常郵件正文閱讀器未找到媒體類型=多部分/格式數據;邊界= ------ ####getMediaType()調用客戶端返回multipart/form-data; boundary = ------ ####

使用上述客戶端解析這個多部分響應的正確方法是什麼?

回答

1

快速的Google搜索爲您提供結果。您必須啓用MultiPartFeature和你做response.readEntity(InputStream.class)

得到了http://www.benchresources.net/jersey-2-x-web-service-for-uploadingdownloading-zip-file-java-client/

// invoke service after setting necessary parameters 
     clientConfig = new ClientConfig(); 
     clientConfig.register(MultiPartFeature.class); 
     client = ClientBuilder.newClient(clientConfig); 
     client.property("accept", "application/zip"); 
     webTarget = client.target(httpURL); 

     // invoke service 
     invocationBuilder = webTarget.request(); 
     //   invocationBuilder.header("Authorization", "Basic " + authorization); 
     response = invocationBuilder.get(); 

     // get response code 
     responseCode = response.getStatus(); 
     System.out.println("Response code: " + responseCode); 

     if (response.getStatus() != 200) { 
      throw new RuntimeException("Failed with HTTP error code : " + responseCode); 
     } 

     // get response message 
     responseMessageFromServer = response.getStatusInfo().getReasonPhrase(); 
     System.out.println("ResponseMessageFromServer: " + responseMessageFromServer); 

     // read response string 
     inputStream = response.readEntity(InputStream.class); 
     qualifiedDownloadFilePath = DOWNLOAD_FILE_LOCATION + "MyJerseyZippedFile.zip"; 
     outputStream = new FileOutputStream(qualifiedDownloadFilePath); 
     byte[] buffer = new byte[1024]; 
     int bytesRead; 
     while ((bytesRead = inputStream.read(buffer)) != -1) { 
      outputStream.write(buffer, 0, bytesRead); 
     } 
+0

由於我在我的回覆中有一個Json,後面跟着一個PDF,client.property中的數據應該是什麼? – amitdonanand

+0

無論API的「@products」包含什麼都可以。你可以忽略它 – sinu

0

我希望以下代碼能幫助你。

Client client = Client.create(); 
client.addFilter(new HTTPBasicAuthFilter(username,password)) 
WebResource webResource = client.resource("URL"); 
ClientResponse response = webResource.accept("*/*").type(MediaType.APPLICATION_OCTET_STREAM).get(ClientResponse.class); 
Assert.assertEquals(response.getStatus(), 200); 
MultivaluedMap<String, String> headers = response.getHeaders(); 
System.out.println("Content-Disposition :" + headers.get("Content-Disposition")); 
List<String> filename=headers.get("Content-Disposition"); 
file_name=filename.get(0); 
file_name=file_name.substring(file_name.indexOf("\"")+1,file_name.lastIndexOf("\"")); 
File file=new File(file_name); 
if(!file.exists()) { 
    file.createNewFile(); 
} 
InputStream inputStream=response.getEntityInputStream(); 
FileOutputStream fileStream = 
     new FileOutputStream(file); 
IOUtils.copy(inputStream, fileStream); 
fileStream.flush(); 
fileStream.close(); 
Assert.assertTrue(file.length()>0); 
//Deleting the backup file 
file.delete(); 
+0

什麼是webResource的數據類型下面的代碼? – amitdonanand