我被要求在android中使用MultiPartEntity寻找下载文件。然而,我搜索了很多,但找不到任何可以使用MultiPartEntity下载文件的解决方案。它似乎只用于上传文件。
我找不到详细的描述,无论它是否用于下载。我只好无奈地问了斯塔克弗流的问题。如果是这样,任何人都可以共享相同的链接或代码片段。如果没有,请也告诉这些信息。我会很高兴的。事先非常感谢。
您可以从URL下载文件的内容,如下所示,
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(/*Download URL*/);
/* If you want custom timeout time use this
int TIMEOUT_MILLIS = 1000;
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(TIMEOUT_MILLIS).setConnectTimeout(TIMEOUT_MILLIS).setConnectionRequestTimeout(TIMEOUT_MILLIS).build();
*/
RequestConfig requestConfig = RequestConfig.DEFAULT;
httpPost.setConfig(requestConfig);
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
HttpEntity httpEntity = builder.build();
httpPost.setEntity(httpEntity);
HttpResponse response = httpClient.execute(httpPost);
String responseLine = null;
if (response != null && response.getStatusLine().getStatusCode() == 200) {
BufferedReader bufferedReader = null;
InputStreamReader streamReader = null;
String line = null;
try {
streamReader = new InputStreamReader(response.getEntity().getContent());
bufferedReader = new BufferedReader(streamReader);
StringBuilder responseBuilder = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
responseBuilder.append(line);
}
responseLine = responseBuilder.toString();
} catch (Exception exception) {
//Handle Exceptions
} finally {
bufferedReader.close();
streamReader.close();
}
}