我想休息使用球衣客戶端獲取呼叫。基於api文檔,請求將圖像作爲二進制數據返回。當我休息時使用郵遞員打電話,我可以回到實際圖像(即時通訊郵遞員將二進制文件轉換回圖像/ PNG)。這是郵遞員返回的以下郵件頭。如何使用澤西客戶端從獲取響應中獲取二進制數據?
我嘗試使其餘獲取Java中使用的球衣客戶端。這裏是我的代碼:
private Client client = ClientBuilder.newClient(new ClientConfig().register(LoggingFilter.class).register(MultiPartFeature.class));
private WebTarget myServer;
myServer= client.target(baseURL);
public void restGetImage(String requestURL, String headers) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
MultivaluedMap<String, Object> userHeaders = storeHeadersInMap(headers);
WebTarget target = getWebTarget().path(requestURL);
Response response = target.request(MediaType.APPLICATION_OCTET_STREAM)
.headers(userHeaders)
.get();
System.out.println("Reuqest URL: " + session.get("baseurl") + requestURL);
int responseCode = response.getStatus();
InputStream inputStream = response.readEntity(InputStream.class);
String contentType = response.getHeaderString("Content-Type");
// br = new BufferedReader(new InputStreamReader(inputStream));
//
// try {
// while((line = br.readLine()) != null) {
// sb.append(line);
// }
//
// br.close(); //close buffered reader
//
// } catch (IOException e) {
// e.printStackTrace();
//
// }
//
// RestResponse data = new RestResponse(responseCode, sb.toString(), contentType);
// data.setResponseHeader(response.getHeaders());
//
// System.out.println("response code: " + responseCode);
// System.out.println("response Content-Type: " + contentType);
// System.out.println("Response body: " + sb.toString());
// return data;
}
請原諒所有評論,因爲我仍在測試代碼。基本上,代碼返回200響應狀態,但是當它無法讀取響應時。它在InputStream inputStream = response.readEntity(InputStrean.class)上引發異常。
Apr 22, 2016 11:37:29 AM
org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderIn
terceptor aroundReadFrom
SEVERE: MessageBodyReader not found for media type=image/png, type=class
com.itextpdf.text.pdf.codec.Base64$InputStream, genericType=class
com.itextpdf.text.pdf.codec.Base64$InputStream.
我的目標是能夠讀取二進制響應數據。任何見解都是值得讚賞的。謝謝。
omg你的權利!非常感謝。我這樣一個愚蠢的錯誤。我很困惑。 – simhuang