2016-09-19 4 views
0

jerseyクライアントを使用して、応答の最初と2番目の部分としてMulipartにJsonとPDFファイルを返すAPIへのREST呼び出しを行っています。ジャージクライアントfor Mulipartコール

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ファイルで文字列レスポンスを返します。 MultiPartクラスとしてエンティティを読み取ろうとしましたが、例外が発生しましたメッセージボディリーダーメディアタイプ= multipart/form-data; boundary = ------ ####およびクライアントの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の '@ produce'には何でも含まれます。あなたはそれを無視するかもしれない – 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

から以下のコードを手に入れたのですか? – amitdonanand