腾讯云视频上传问题总结

使用腾讯云提供java sdk4.4
MultipartUploadVodFile使用此接口,分片上传,分片大小设置1024*1024*20会被腾讯服务器拒绝,无法上传,
目前设置为1024*1024*5
最小分片设置为1024*512


/**
     * 域名:vod.qcloud.com
     * 接口名: MultipartUploadVodFile
     * @Description:本地上传至腾讯云
     * @param
     * @return
     * @author zhiwei.yan
     * @date 2017/8/16 0016 8:49
     */
    public static String localToTcCloud (UploadParam uploadParam) {
        QcloudApiModuleCenter module = getModule("POST");
        try{
            System.out.println("starting...");
            String fileName = uploadParam.getFileLocalPath();
            //文件大小
            long fileSize = new File(fileName).length();
            //文件sha1值
            String fileSHA1 = SHA1.fileNameToSHA(fileName);
            //每次上传字节数,可自定义
            int fixDataSize = 1024*1024*5;
            //切片上传:最小片字节数(默认不变),如果:dataSize + offset > fileSize,把这个值变小即可
            int firstDataSize = 1024*512;
            //
            int tmpDataSize = firstDataSize;
            long remainderSize = fileSize;
            int tmpOffset = 0;
            int code, flag;
            String fileId = null;
            String result = null;
            if(remainderSize<=0){
                System.out.println("wrong file path...");
            }
            long startTime = System.currentTimeMillis();
            while (remainderSize>0) {
                TreeMap params = new TreeMap();

                params.put("fileSha", fileSHA1);
                params.put("fileType", uploadParam.getFileType());
                params.put("fileName", uploadParam.getFileName());
                params.put("fileSize", fileSize);
                params.put("dataSize", tmpDataSize);
                params.put("offset", tmpOffset);
                params.put("file", fileName);
                params.put("isTranscode", 0);
                params.put("isScreenshot", 0);
                params.put("isWatermark", 0);

                result = module.call("MultipartUploadVodFile", params);
                System.out.println(result);
                JSONObject json_result = new JSONObject(result);
                code = json_result.getInt("code");
                if (code == -3002) {               //服务器异常返回,需要重试上传(offset=0, dataSize=10K,满足大多数视频的上传)
                    tmpDataSize = firstDataSize;
                    tmpOffset = 0;
                    continue;
                } else if (code != 0) {
                    return null;
                }
                flag = json_result.getInt("flag");
                if (flag == 1) {
                    fileId = json_result.getString("fileId");
                    break;
                } else {
                    tmpOffset = Integer.parseInt(json_result.getString("offset"));
                }
                remainderSize = fileSize - tmpOffset;
                if (fixDataSize < remainderSize) {
                    tmpDataSize = fixDataSize;
                } else {
                    tmpDataSize = (int) remainderSize;
                }
                long costTime = System.currentTimeMillis() - startTime;
                LOG.info("-------this video cost time : " + costTime + "ms," + costTime / 1000 + "s");
            }
            long totalTime = System.currentTimeMillis() - startTime;
            LOG.info("-------this video cost time : " + totalTime + "ms," + totalTime / 1000 + "s");
            System.out.println("end...");
            return result;
        }
        catch (Exception e) {
            e.printStackTrace();
            System.out.println("error..."+e.toString());
        }
        return null;
    }

有一份测试完整的代码,抽时间整理到github上
完整代码地址:https://github.com/yanhua711/morefeels
仅供参考,有问题欢迎一起研究

你可能感兴趣的:(java)