Exemple #1
0
 /**
  * 七牛上传
  *
  * @param bytes
  * @param key
  * @return 下载链接
  * @throws QiniuException
  */
 public String upload(byte[] bytes, String key) throws QiniuException {
   String token = this.getToken();
   UploadManager uploadManager = new UploadManager();
   Response res = uploadManager.put(bytes, key, token);
   if (res.isOK()) {
     // 获取私有下载 URL
     // String downloadUrl = auth.privateDownloadUrl("http://" + domain + "/" + fileName);
     return domain + "/" + key;
   }
   return null;
 }
Exemple #2
0
  public static void upLoadFile(byte[] file, String key) {
    try {

      Response res = uploadManager.put(file, key, auth.uploadToken("xizhimojie"));
      // log.info(res);
      // log.info(res.bodyString());
      // Ret ret = res.jsonToObject(Ret.class);
      if (res.isOK()) {
        log.info(key + "上传成功!");
      } else {
        log.error(key + "上传失败!");
      }
    } catch (QiniuException e) {
      Response r = e.response;
      // 请求失败时简单状态信息
      log.error(r.toString());
      try {
        // 响应的文本信息
        log.error(r.bodyString());
      } catch (QiniuException e1) {
        // ignore
      }
    }
  }
  public Response upload() throws QiniuException {
    long uploaded = helper.recoveryFromRecord();
    try {
      this.file = new FileInputStream(f);
    } catch (FileNotFoundException e) {
      throw new QiniuException(e);
    }
    boolean retry = false;
    int contextIndex = blockIdx(uploaded);
    while (uploaded < size) {
      int blockSize = nextBlockSize(uploaded);
      try {
        file.read(blockBuffer, 0, blockSize);
      } catch (IOException e) {
        close();
        throw new QiniuException(e);
      }

      //            long crc = Crc32.bytes(blockBuffer, 0, blockSize);
      Response response = null;
      try {
        response = makeBlock(blockBuffer, blockSize);
      } catch (QiniuException e) {
        if (e.code() < 0) {
          host = Config.zone.upHostBackup;
        }
        if (e.response == null || e.response.needRetry()) {
          retry = true;
        } else {
          close();
          throw e;
        }
      }
      if (retry) {
        try {
          response = makeBlock(blockBuffer, blockSize);
          retry = false;
        } catch (QiniuException e) {
          close();
          throw e;
        }
      }
      ResumeBlockInfo blockInfo = response.jsonToObject(ResumeBlockInfo.class);
      // TODO check return crc32
      // if blockInfo.crc32 != crc{}

      contexts[contextIndex++] = blockInfo.ctx;
      uploaded += blockSize;
      helper.record(uploaded);
    }
    close();

    try {
      return makeFile();
    } catch (QiniuException e) {
      try {
        return makeFile();
      } catch (QiniuException e1) {
        throw e1;
      }
    } finally {
      helper.removeRecord();
    }
  }