Android 断点续传功能

断点续传功能,在文件上传中断时,下次上传同一文件时,能在上次的断点处继续上传,可节省时间和流量

总结规划步骤:

1.给大文件分片,每一个大文件发送前,相对应的创建一个文件夹存放所有分片

2.上传接口,每一个分片上传完成就删掉,直到所有分片上传完成,再删掉存放分片的文件夹,服务器把分片合成完整的文件。

先看分片功能,传入的3个参数分别为源文件地址,分片大小,存放分片的文件夹地址。返回的是分片个数。

    /**
     *
     * @param sourceFilePath    源文件地址
     * @param partFileLength    分割文件的每一个片段大小标准
     * @param splitPath         分割之后片段所在文件夹
     * @return
     * @throws Exception
     */
    public static int splitFile(String sourceFilePath, int partFileLength, String splitPath) throws Exception {
        File sourceFile = null;
        File targetFile = null;
        InputStream ips = null;
        OutputStream ops = null;
        OutputStream configOps = null;//该文件流用于存储文件分割后的相关信息,包括分割后的每个子文件的编号和路径,以及未分割前文件名
        Properties partInfo = null;//properties用于存储文件分割的信息
        byte[] buffer = null;
        int partNumber = 1;
        sourceFile = new File(sourceFilePath);//待分割文件
        ips = new FileInputStream(sourceFile);//找到读取源文件并获取输入流
        //创建一个存放分片的文件夹
        File tempFile = new File(splitPath);
        if (!tempFile.exists()) {
            tempFile.mkdirs();
        }
        configOps = new FileOutputStream(new File(tempFile.getAbsolutePath() + File.separator + "config.properties"));
        buffer = new byte[partFileLength];//开辟缓存空间
        int tempLength = 0;
        partInfo = new Properties();//key:1开始自动编号 value:文件路径

        int sliceCount = 0;
        while ((tempLength = ips.read(buffer, 0, partFileLength)) != -1) {
            String targetFilePath = tempFile.getAbsolutePath() + File.separator + "part_" + (partNumber);//分割后的文件路径+文件名
            sliceCount = partNumber;
            partInfo.setProperty((partNumber++) + "", targetFilePath);//将相关信息存储进properties
            targetFile = new File(targetFilePath);
            ops = new FileOutputStream(targetFile);//分割后文件
            ops.write(buffer, 0, tempLength);//将信息写入碎片文件

            ops.close();//关闭碎片文件
        }
        partInfo.setProperty("name", sourceFile.getName());//存储源文件名
        partInfo.setProperty("sliceCount", sliceCount + "");//存储分片个数
        partInfo.store(configOps, "ConfigFile");//将properties存储进实体文件中
        ips.close();//关闭源文件流

        return sliceCount;
    }

接下来,和服务器协商接口,一共2个接口

1.uploadLargeFilePre,传入参数除了常规上传文件参数之外加了分片个数sliceCount和fileHashcode。这一步是给服务器开辟存放分片的控件的,不执行上传操作,文件哈希值作为识别分片归属的唯一标准。返回int类型rCode,根据rCode结果判断是否执行第2步上传动作

2.uploadLargeFile,传入参数除了常规上传文件参数之外加了分片path,分片index,isFinished。isFinished是最后一片的依据。客户端就在上传接口的response里继续上传下一个分片,直到最后一片上传完成,服务器才会返给这个大文件的url。

看代码

protected void addFileMsg(String path) {
        forceLogin();

        if (path == null) {
            return;
        }
        File file = new File(path);
        if (file.exists()) {
            ImMsg msg = new ImMsg();
            msg.setType(ImMsg.MSG_TYPE_FILE);
            msg.setTime(System.currentTimeMillis());
            msg.setMsgReaded();
            msg.setReceiveTime(System.currentTimeMillis());
            AccountInfo accountInfo = IMApp.instance().getAccountInfo();
            msg.setUserId(accountInfo.getUserId());
            msg.setSex(accountInfo.getSex());
            msg.setUserName(accountInfo.mNickName);
            msg.setHeadUrl(accountInfo.mHead);
            msg.mMasterId = mMasterId;
            msg.setMsg(new File(path).getName());
            msg.setPicPath(path);
            msg.setState(ImMsg.STATE_SEND_UPLOADING);
            String ext = null;
            if (path.endsWith("doc") || path.endsWith("docx")) {
                ext = "doc";
            } else if (path.endsWith("xls") || path.endsWith("xlsx")) {
                ext = "xls";
            } else if (path.endsWith("ppt") || path.endsWith("pptx")) {
                ext = "ppt";
            } else if (path.endsWith("pd
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值