2017-04-03 39 views

回答

0

我能夠做到這一點,按照這個答案Receiving Multipart Response on client side (ClosableHttpResponse)中描述的內容。 我輸入下面的庫:

編譯 'com.sun.mail:機器人郵箱:1.5.5'

所以我的代碼現在是這樣的:

Request request = new Request.Builder() 
     .url(url) 
     .addHeader("range", String.format("bytes=%s", TextUtils.join(", ", ranges))) 
     .build(); 
Response response = client.newCall(request).execute(); 
ByteArrayDataSource dataSource = new ByteArrayDataSource(response.body().byteStream(), response.body().contentType().toString()); 
MimeMultipart multipart = new MimeMultipart(dataSource); 
int count = multipart.getCount(); 
for (int i = 0; i < count; i++) { 
    BodyPart bodyPart = multipart.getBodyPart(i); 
    if (bodyPart.isMimeType("application/octet-stream")) { 
     processBinaryStream(bodyPart.getInputStream()); 
    } else { 
     // Or process different types of data 
     throw new Exception(String.format("Content type: %s cannot be parsed", bodyPart.getContentType())); 
    } 
} 

我仍然不滿意,不得不導入整個郵件處理庫,只是爲了管理多部分響應。但是現在,問題一直存在,直到我找到或想出更好的解決方案。