com.qiniu.http.Response类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(483)

本文整理了Java中com.qiniu.http.Response类的一些代码示例,展示了Response类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Response类的具体详情如下:
包路径:com.qiniu.http.Response
类名称:Response

Response介绍

[英]定义HTTP请求的日志信息和常规方法
[中]定义超文本传输协议请求的日志信息和常规方法

代码示例

代码示例来源:origin: nice-swa/my-site

public String upload(MultipartFile file, String fileName) {
  //构造一个带指定Zone对象的配置类
  Configuration cfg = new Configuration(Zone.zone0());
  //...其他参数参考类注释
  UploadManager uploadManager = new UploadManager(cfg);
  //默认不指定key的情况下,以文件内容的hash值作为文件名
  String key = null;
  Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
  String upToken = auth.uploadToken(BUCKET);
  try {
    Response response = null;
    response = uploadManager.put(file.getInputStream(), fileName, upToken, null, null);
    //解析上传成功的结果
    DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
    System.out.println(putRet.key);
    System.out.println(putRet.hash);
    return putRet.key;
  } catch (QiniuException ex) {
    Response r = ex.response;
    System.err.println(r.toString());
    try {
      System.err.println(r.bodyString());
    } catch (QiniuException ex2) {
      //ignore
    }
  } catch (IOException e) {
    e.printStackTrace();
  }
  return null;
}

代码示例来源:origin: Alluxio/alluxio

/**
 * Deletes Object in Qiniu kodo.
 * @param key Object key
 */
public void deleteObject(String key) throws QiniuException {
 com.qiniu.http.Response response = mBucketManager.delete(mBucketName, key);
 response.close();
}
/**

代码示例来源:origin: qiniu/java-sdk

public FileListing listFiles(String bucket, String prefix, String marker, int limit, String delimiter)
    throws QiniuException {
  Response response = listV1(bucket, prefix, marker, limit, delimiter);
  if (!response.isOK()) {
    throw new QiniuException(response);
  }
  FileListing fileListing = response.jsonToObject(FileListing.class);
  response.close();
  return fileListing;
}

代码示例来源:origin: yzcheng90/X-SpringBoot

@Override
public String upload(byte[] data, String path) {
  try {
    Response res = uploadManager.put(data, path, token);
    if (!res.isOK()) {
      throw new RuntimeException("上传七牛出错:" + res.toString());
    }
  } catch (Exception e) {
    throw new RRException("上传文件失败,请核对七牛配置信息", e);
  }
  return config.getQiniuDomain() + "/" + path;
}

代码示例来源:origin: zhangyd-c/OneBlog

public FileInfo getFileInfo(String fileName) {
  BucketManager bucketManager = new BucketManager(this.auth);
  FileInfo info = null;
  try {
    info = bucketManager.stat(config.getQiniuBucketName(), fileName);
    log.info(info.hash);
    log.info(info.key);
  } catch (QiniuException e) {
    Response r = e.response;
    log.error(r.toString(), e);
  }
  return info;
}

代码示例来源:origin: qiniu/java-sdk

public void deleteBucket(String bucketname) throws QiniuException {
  String url = String.format("%s/drop/%s", configuration.rsHost(), bucketname);
  Response res = post(url, null);
  if (!res.isOK()) {
    throw new QiniuException(res);
  }
  res.close();
}

代码示例来源:origin: vakinge/jeesuite-libs

/**
 * 处理上传结果,返回文件url
 * 
 * @return
 * @throws QiniuException
 */
private String processUploadResponse(Response res) throws QiniuException {
  if (res.isOK()) {
    UploadResult ret = res.jsonToObject(UploadResult.class);
    return getFullPath(ret.key);
  }
  throw new FSOperErrorException(name(), res.toString());
}

代码示例来源:origin: qiniu/java-sdk

public String getInfo() {
    String[] msg = new String[3];
    try {
      msg[0] = url();
    } catch (Throwable t) {
    }
    try {
      msg[1] = toString();
    } catch (Throwable t) {
    }
    try {
      msg[2] = bodyString();
    } catch (Throwable t) {

    }

    return StringUtils.join(msg, "  \n");
  }
}

代码示例来源:origin: Jamling/SmartIM

com.qiniu.http.Response response = uploadManager
    .put(file.getAbsolutePath(), key, authInfo.token);
if (!response.isOK()) {
  throw new RuntimeException(
      response.error + "(code=" + response.statusCode + ")");
UploadInfo putRet = new Gson().fromJson(response.bodyString(),
    UploadInfo.class);
if (authInfo.domain != null && !authInfo.domain.isEmpty()) {

代码示例来源:origin: qiniu/java-sdk

public StringMap jsonToMap() throws QiniuException {
  if (!isJson()) {
    return null;
  }
  String b = bodyString();
  return Json.decode(b);
}

代码示例来源:origin: zhangyd-c/OneBlog

private String upload(Response res) throws IOException {
  try {
    int status = res.statusCode;
    if (status == CommonConst.DEFAULT_SUCCESS_CODE) {
      StringMap map = res.jsonToMap();
      return String.valueOf(map.get("key"));
    }
  } catch (QiniuException e) {
    Response r = e.response;
    log.error(r.toString(), e);
  }
  return null;
}

代码示例来源:origin: scruel/ClipIt

private static String parserQiniuResponseResult(Response response) throws QiniuException {
    //解析上传成功的结果
    DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
    String prefix = properties.getProperty("markdown.prefix");
    String suffix = properties.getProperty("markdown.suffix");
    String result;
    String key = putRet.key;
    String fileName = key.substring(key.indexOf("_", key.indexOf("_") + 1) + 1);
    if (!fileName.startsWith("clipboard")) {
      prefix = prefix.replace("image", fileName);
    }
    try {
      result = String.format("%s%s%s", prefix, bucketDomain + "/" + URLEncoder.encode(key, "utf-8").replace("+", "%20"), suffix);

    } catch (UnsupportedEncodingException e) {
      // e.printStackTrace();
      result = String.format("%s%s%s", prefix, bucketDomain + "/" + key, suffix);
    }
    // System.out.println(result);
    return result;
  }
}

代码示例来源:origin: qiniu/java-sdk

@Test
public void testBatchStat() {
  Map<String, String> bucketKeyMap = new HashMap<String, String>();
  bucketKeyMap.put(TestConfig.testBucket_z0, TestConfig.testKey_z0);
  bucketKeyMap.put(TestConfig.testBucket_na0, TestConfig.testKey_na0);
  for (Map.Entry<String, String> entry : bucketKeyMap.entrySet()) {
    String bucket = entry.getKey();
    String key = entry.getValue();
    String[] keyArray = new String[100];
    for (int i = 0; i < keyArray.length; i++) {
      keyArray[i] = key;
    }
    BucketManager.BatchOperations ops = new BucketManager.BatchOperations()
        .addStatOps(bucket, keyArray);
    try {
      Response r = bucketManager.batch(ops);
      BatchStatus[] bs = r.jsonToObject(BatchStatus[].class);
      Assert.assertTrue("200 or 298", batchStatusCode.contains(bs[0].code));
    } catch (QiniuException e) {
      Assert.fail(e.response.toString());
    }
  }
}

代码示例来源:origin: qiniu/java-sdk

new FileInputStream(f), null, mime, c);
  Response r = up.upload();
  StreamUploadTest.MyRet ret = r.jsonToObject(StreamUploadTest.MyRet.class);
  assertEquals(expectKey, ret.key);
  assertEquals(String.valueOf(f.length()), ret.fsize);
  assertEquals(etag, ret.hash);
} catch (QiniuException e) {
  assertEquals("", e.response == null ? "e.response is null" : e.response.bodyString());
  fail();

代码示例来源:origin: qiniu/java-sdk

public FileListing listFilesV2(String bucket, String prefix, String marker, int limit, String delimiter)
    throws QiniuException {
  Response response = listV2(bucket, prefix, marker, limit, delimiter);
  final String result = response.bodyString();
  response.close();
  List<String> lineList = Arrays.asList(result.split("\n"));
  FileListing fileListing = new FileListing();
  List<FileInfo> fileInfoList = new ArrayList<>();
  Set<String> commonPrefixSet = new HashSet<>();
  for (int i = 0; i < lineList.size(); i++) {
    String line = lineList.get(i);
    JsonObject jsonObject = Json.decode(line, JsonObject.class);
    if (!(jsonObject.get("item") instanceof JsonNull))
      fileInfoList.add(Json.decode(jsonObject.get("item"), FileInfo.class));
    String dir = jsonObject.get("dir").getAsString();
    if (!"".equals(dir)) commonPrefixSet.add(dir);
    if (i == lineList.size() - 1)
      fileListing.marker = jsonObject.get("marker").getAsString();
  }
  fileListing.items = fileInfoList.toArray(new FileInfo[]{});
  fileListing.commonPrefixes = commonPrefixSet.toArray(new String[]{});
  return fileListing;
}

代码示例来源:origin: hexiangtao/wechat4j

public String uploadToQiniu(byte[] bytes, String originalName) {
  try {
    Auth auth = Auth.create(ak, secret);
    String uploadToken = auth.uploadToken(bucket);
    String fileKey = generateFileKey(originalName);
    Response response = uploadManager.put(bytes, fileKey, uploadToken);
    if (!response.isOK()) {
      return "";
    }
    String host = Constant.configReader.get(Constant.QIUNIU_IMG_URL);
    return host + fileKey;
  } catch (Exception ex) {
    ex.printStackTrace();
    return "";
  }
}

代码示例来源:origin: qiniu/java-sdk

UploadManager uploadManager = new UploadManager(new Configuration(zone));
  Response res = uploadManager.put(new FileInputStream(f), expectKey, token, params, null);
  StringMap m = res.jsonToMap();
  assertEquals("foo_val", m.get("foo"));
} catch (QiniuException e) {
  assertEquals("", e.response == null ? "e.response is null" : e.response.bodyString());
  fail();
} finally {

代码示例来源:origin: qiniu/java-sdk

/**
 * 通过 API 接口查询上传域名
 */
private UCRet getZoneJson(ZoneIndex index) throws QiniuException {
  String address = ucServer + "/v1/query?ak=" + index.accessKey + "&bucket=" + index.bucket;
  Response r = client.get(address);
  return r.jsonToObject(UCRet.class);
}

代码示例来源:origin: qiniu/java-sdk

public QiniuException(Response response) {
  super(response != null ? response.getInfo() : null);
  this.response = response;
  if (response != null) {
    response.close();
  }
}

代码示例来源:origin: qiniu/java-sdk

public String error() {
    if (error != null) {
      return error;
    }
    if (response == null || response.statusCode / 100 == 2 || !response.isJson()) {
      return null;
    }
    Error e = null;
    try {
      e = response.jsonToObject(Error.class);
    } catch (QiniuException e1) {
      e1.printStackTrace();
    }
    error = e == null ? "" : e.error;
    return error;
  }
}

相关文章