private String getFileContents(Drive drive, String downloadUrl) {
    if (!TextUtils.isEmpty(downloadUrl)) {
      try {
        HttpResponse resp =
            drive.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl)).execute();

        final char[] buffer = new char[1024];
        final StringBuilder out = new StringBuilder();
        Reader in = null;
        try {
          in = new InputStreamReader(resp.getContent(), "UTF-8");
          for (; ; ) {
            int rsz = in.read(buffer, 0, buffer.length);
            if (rsz < 0) break;
            out.append(buffer, 0, rsz);
          }
        } finally {
          in.close();
        }
        return out.toString();
      } catch (IOException e) {
        Log.e(getClass().getSimpleName(), "Error downloading file contenst", e);
      }
    }
    return null;
  }
示例#2
0
  /**
   * Retrieve a Google Drive file's content.
   *
   * @param driveFile Google Drive file to retrieve content from.
   * @return Google Drive file's content if successful, {@code null} otherwise.
   */
  public String getFileContent(File driveFile) {
    String result = "";

    if (driveFile.getDownloadUrl() != null && driveFile.getDownloadUrl().length() > 0) {
      try {
        GenericUrl downloadUrl = new GenericUrl(driveFile.getDownloadUrl());

        HttpResponse resp = mService.getRequestFactory().buildGetRequest(downloadUrl).execute();
        InputStream inputStream = null;

        try {
          inputStream = resp.getContent();
          BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
          StringBuilder content = new StringBuilder();
          char[] buffer = new char[1024];
          int num;

          while ((num = reader.read(buffer)) > 0) {
            content.append(buffer, 0, num);
          }
          result = content.toString();
        } finally {
          if (inputStream != null) {
            inputStream.close();
          }
        }
      } catch (IOException e) {
        // An error occurred.
        e.printStackTrace();
        return null;
      }
    } else {
      // The file doesn't have any content stored on Drive.
      return null;
    }

    return result;
  }
  protected InputStream getContentStream(String downloadURL) throws PortalException {

    if (Validator.isNull(downloadURL)) {
      return null;
    }

    Drive drive = getDrive();

    HttpRequestFactory httpRequestFactory = drive.getRequestFactory();

    GenericUrl genericUrl = new GenericUrl(downloadURL);

    try {
      HttpRequest httpRequest = httpRequestFactory.buildGetRequest(genericUrl);

      HttpResponse httpResponse = httpRequest.execute();

      return httpResponse.getContent();
    } catch (IOException ioe) {
      _log.error(ioe, ioe);

      throw new SystemException(ioe);
    }
  }