httpclient 分片上传文件_java上传微博视频素材

5d7fa525d9c05ec6e0604e91a668352d.png

在对接微博广告时,微博给出的代码示例是PHP版本的,没有java版本,因此对接时有两个地方容易出问题,

1、视频分片上传

2、视频分片后MD5取值验证不通过。

查询当前网站没有提供相应的参考内容。

注意事项:如果不分片时,初始化接口的MD5值需要和分片MD5值相同。

下面将自己对接时的源码分享出来供大家参考。

视频上传核心代码:Map initParamsMap = initToken();        AdxCreativeListInfo creativeListInfo = creativeListInfoList.get(0);        initParamsMap.put(WeiboConstants.VideoUpload.NAME, creativeListInfo.getCname());        initParamsMap.put(WeiboConstants.VideoUpload.CHECK, creativeListInfo.getfMd5());        Integer size = creativeListInfo.getSize();        initParamsMap.put(WeiboConstants.VideoUpload.LENGTH, size);        try {            log.info("上传视频-初始化接口,请求参数。");            JSONObject initRetJo = CommonHttpUtil.postJsonResponseObject(weiboConfig.getVideoUploadInitUrl(), initParamsMap);            if (!RetCodeEnum.checkSuccess(initRetJo.getInteger(WeiboConstants.RET_CODE))) {                log.error("上传视频-初始化接口,返回报错。retMst={}", initRetJo.getString(WeiboConstants.RET_MSG));                return;            }            JSONObject initMsg = initRetJo.getJSONObject(WeiboConstants.RET_MSG);            String uploadId = initMsg.getString(WeiboConstants.VideoUpload.UPLOAD_ID);            Integer length = initMsg.getInteger(WeiboConstants.VideoUpload.LENGTH);            File file = new File(tempFilePath + "upload_video/" + creativeListInfo.getCampaignId() + "_" + creativeListInfo.getCname());            InputStream inputStream = new URL(creativeListInfo.getValue()).openStream();            String weiboUrl = StringUtils.EMPTY;            Byte uploadStatus = null;            try {                //计算文件分片数                int count = size % length > 0 ? size / length + 1 : size / length;                //获取文件                FileUtils.copyInputStreamToFile(inputStream, file);                Map uploadParamsMap = initToken();                uploadParamsMap.put(WeiboConstants.VideoUpload.UPLOAD_ID, uploadId);                uploadParamsMap.put(WeiboConstants.VideoUpload.LENGTH, length);                uploadParamsMap.put(WeiboConstants.VideoUpload.FILE_LENGTH, size);                uploadParamsMap.put(WeiboConstants.VideoUpload.CHECK, creativeListInfo.getfMd5());                for (int i = 0; i < count; i++) {                    uploadParamsMap.put(WeiboConstants.VideoUpload.PART_NUMBER, i);                    byte[] block = CommonFileUtils.getBlock(file, i, length);                    uploadParamsMap.put(WeiboConstants.VideoUpload.CONTENT, Base64.getEncoder().encodeToString(block));                    uploadParamsMap.put(WeiboConstants.VideoUpload.SLICE_CHECK, MD5Utils.phpMd5(block));                    log.info("上传视频-调用上传接口,请求参数。");                    JSONObject uploadRetJo = CommonHttpUtil.postJsonResponseObject(weiboConfig.getVideoUploadUrl(), uploadParamsMap);                    Integer errCode = uploadRetJo.getInteger(WeiboConstants.ERR_CODE);                    if (!RetCodeEnum.checkSuccess(uploadRetJo.getInteger(WeiboConstants.RET_CODE)) || 0 != errCode) {                        log.error("上传视频-调用上传接口,上传错误。erroCode={}[{}],retMsg={}", errCode, errCodeMap.get(String.valueOf(errCode)), uploadRetJo.getString(WeiboConstants.RET_MSG));                        uploadStatus = UploadStatusEnum.Failed_Upload.getStatus();                        break;                    }                    JSONObject uploadMsg = uploadRetJo.getJSONObject(WeiboConstants.RET_MSG);                    weiboUrl = uploadMsg.getString(WeiboConstants.VideoUpload.URL);                    uploadStatus = UploadStatusEnum.Success_Upload.getStatus();                }                AdxAppWeiboVideo update = new AdxAppWeiboVideo();                update.setId(video.getId());                update.setVideoUrl(weiboUrl);                update.setUploadStatus(uploadStatus);                update.setUploadDate(new Date());                adxAppWeiboVideoService.updateBySelective(update);            } catch (Exception e) {                log.error("上传视频-调用上传接口,异常", e);            } finally {                file.delete();                inputStream.close();            }        } catch (Exception e) {            log.error("上传视频-初始化接口,报错", e);        }

其中参考封装方法

1、视频分片

byte[] block = CommonFileUtils.getBlock(file, i, length);
/**     * 获取分块文件数组     *     * @param file      文件     * @param offset    起始偏移量     * @param blockSize 分块大小     * @return     */    public static byte[] getBlock(File file, long offset, int blockSize) {        byte[] result = new byte[blockSize];        RandomAccessFile accessFile = null;        try {            accessFile = new RandomAccessFile(file, "r");            accessFile.seek(offset * blockSize);            int readSize = accessFile.read(result);            if (readSize == -1) {                return null;            } else if (readSize == blockSize) {                return result;            } else {                byte[] tmpByte = new byte[readSize];                System.arraycopy(result, 0, tmpByte, 0, readSize);                return tmpByte;            }        } catch (IOException e) {            e.printStackTrace();        } finally {            if (accessFile != null) {                try {                    accessFile.close();                } catch (IOException e1) {                }            }        }        return null;    }

2、md5加密

MD5Utils.phpMd5(block)
/**     * @param input 输入     * @return 返回16个字节     * @throws Exception     */    public static String phpMd5(byte[] input) throws Exception {        MessageDigest md5 = MessageDigest.getInstance("MD5");        md5.update(input);        /** 获取加密后的字节数组 */        byte[] md5Bytes = md5.digest();        String res = "";        for (int i = 0; i < md5Bytes.length; i++){            int temp = md5Bytes[i] & 0xFF;            // 转化成十六进制不够两位,前面加零            if (temp <= 0XF){                res += "0";            }            res += Integer.toHexString(temp);        }        return res;    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值