简体   繁体   English

在 Android 中,多部分实体是否也用于下载文件,因为我看不到 class 的任何示例或详细说明

[英]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.

相关问题 如何使用存储在辅助类中主类中的Strings值? (有关详细信息,请参见描述。Java) - How can I use the Strings values stored in a main class in my secondary class? (see description for details. Java) 我在 JMC 中看不到异常详细信息 - I can't see exception details in JMC Intellij:Javadoc 在 ctrl+b 后我看不到方法的描述,class 在 java 文档中 - Intellij: Javadoc I can't see description of the methods, class in java documentation after ctrl+b 如何修复我的 Android 应用程序在启动时一直崩溃,无法打开的错误? (更多细节见描述) - How to fix a bug where my Android app keeps crashing on start, does not open? (see description for more details) 为什么我在 developer.android.com 中看不到 Kotlin 示例? - Why I can't see Kotlin example in developer.android.com? 从HelloWorld示例ArcGIS android SDK中看不到地图 - Can't see the map from the HelloWorld example ArcGIS android SDK 我无法弄清楚如何重置循环(参见示例) - I can't figure out how to reset a loop (see example) 为什么我在Android Studio中看不到布局预览,却看到一个“十字”符号并且无法添加任何元素? - Why can't I see the layout preview in Android Studio but instead I see a 'cross' sign and am not able to add any elements? 我看不到课程文件 - I can't see class files 413 上传多部分文件时请求实体太大 - 413 Request Entity Too Large while uploading a multipart file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM