栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何从Android中的URL下载文件的一部分?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何从Android中的URL下载文件的一部分?

假设您正在使用HTTP进行下载,则需要使用HEAD http动词和RANGE http标头。

HEAD将为您提供文件大小(如果可用),然后RANGE可让您下载字节范围。

有了文件大小后,将其分成大小大致相等的块,并为每个块生成下载线程。完成所有操作后,以正确的顺序写入文件块。

如果您不知道如何使用RANGE标头,这里是另一个SO答案,说明了如何:http://codingdict.com/questions/131429

[编辑]

为了使文件成块,请使用它并开始下载过程,

private void getBytesFromFile(File file) throws IOException {    FileInputStream is = new FileInputStream(file); //videorecorder stores video to file    java.nio.channels.FileChannel fc = is.getChannel();    java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocate(10000);    int chunkCount = 0;    byte[] bytes;    while(fc.read(bb) >= 0){        bb.flip();        //save the part of the file into a chunk        bytes = bb.array();        storeByteArrayToFile(bytes, mRecordingFile + "." + chunkCount);//mRecordingFile is the (String)path to file        chunkCount++;        bb.clear();    }}private void storeByteArrayToFile(byte[] bytesToSave, String path) throws IOException {    FileOutputStream fOut = new FileOutputStream(path);    try {        fOut.write(bytesToSave);    }    catch (Exception ex) {        Log.e("ERROR", ex.getMessage());    }    finally {        fOut.close();    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/498323.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号