Esempio n. 1
0
  /**
   * 图片语音文件下载
   *
   * @param fileUUID 文件在DB的UUID
   * @param shareSecret 文件在DB中保存的shareSecret
   * @param localPath 下载后文件存放地址
   * @param isThumbnail 是否下载缩略图 true:缩略图 false:非缩略图
   * @return
   */
  public static ObjectNode mediaDownload(
      String fileUUID, String shareSecret, File localPath, Boolean isThumbnail) {
    ObjectNode objectNode = factory.objectNode();
    File downLoadedFile = null;
    if (!JerseyUtils.match("^(?!-)[0-9a-zA-Z\\-]+#[0-9a-zA-Z]+", APPKEY)) {
      LOGGER.error("Bad format of Appkey: " + APPKEY);
      objectNode.put("message", "Bad format of Appkey");
      return objectNode;
    }

    try {
      JerseyWebTarget webTarget =
          EndPoints.CHATFILES_TARGET
              .resolveTemplate("org_name", APPKEY.split("#")[0])
              .resolveTemplate("app_name", APPKEY.split("#")[1])
              .path(fileUUID);
      List<NameValuePair> headers = new ArrayList<NameValuePair>();
      headers.add(new BasicNameValuePair("share-secret", shareSecret));
      headers.add(new BasicNameValuePair("Accept", "application/octet-stream"));
      if (isThumbnail != null && isThumbnail) {
        headers.add(new BasicNameValuePair("thumbnail", String.valueOf(isThumbnail)));
      }
      downLoadedFile = JerseyUtils.downLoadFile(webTarget, credential, headers, localPath);
      LOGGER.error(
          "File download successfully,file path : " + downLoadedFile.getAbsolutePath() + ".");
    } catch (Exception e) {
      e.printStackTrace();
    }

    objectNode.put("message", "File download successfully .");
    return objectNode;
  }
Esempio n. 2
0
  /**
   * 图片/语音文件上传
   *
   * @param uploadFile
   */
  public static ObjectNode mediaUpload(File uploadFile) {

    ObjectNode objectNode = factory.objectNode();

    if (!uploadFile.exists()) {

      LOGGER.error("file: " + uploadFile.toString() + " is not exist!");

      objectNode.put("message", "File or directory not found");

      return objectNode;
    }

    if (!JerseyUtils.match("^(?!-)[0-9a-zA-Z\\-]+#[0-9a-zA-Z]+", APPKEY)) {
      LOGGER.error("Bad format of Appkey: " + APPKEY);

      objectNode.put("message", "Bad format of Appkey");

      return objectNode;
    }

    try {
      JerseyWebTarget webTarget =
          EndPoints.CHATFILES_TARGET
              .resolveTemplate("org_name", APPKEY.split("#")[0])
              .resolveTemplate("app_name", APPKEY.split("#")[1]);

      List<NameValuePair> headers = new ArrayList<NameValuePair>();
      headers.add(new BasicNameValuePair("restrict-access", "true"));

      objectNode = JerseyUtils.uploadFile(webTarget, uploadFile, credential, headers);

    } catch (Exception e) {
      e.printStackTrace();
    }

    return objectNode;
  }