[英]In Android Does multipart entity is used for download file too as I can't see any example or details description for that class
I was asked to look for downloading file using MultiPartEntity in android However i searched a lot but couldn't find any solution where I can download file using MultiPartEntity.我被要求在 android 中查找使用 MultiPartEntity 下载文件但是我搜索了很多但找不到任何可以使用 MultiPartEntity 下载文件的解决方案。 it seems it is used to upload files only .
它似乎只用于上传文件。
I couldn't find detail description whether it does use for download or not.我找不到详细说明它是否用于下载。 so helplessly I had to ask the question on stackoverflow.
如此无奈,我不得不在stackoverflow上提出问题。 If it does can anyone share a link or code snippet for the same.
如果确实如此,任何人都可以共享相同的链接或代码段。 If it doesn't Please tell this information too.
如果不是请也告诉这个信息。 I'll be really glad.
我会很高兴的。 Thank you very much in advance.
非常感谢您提前。
You can download contents of a file from URL as follows,您可以从 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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.