1

请我需要以编程方式读取存储在 Google Drive 中的文件的内容。我期待着某种
InputStream is = <drive_stuff>.read(fileID);
帮助?如果我可以使用某种方式写回文件,我也会很感激

OutputStream dos = new DriveOutputStream(driveFileID);
dos.write(data);

如果这种方便的方法对于 Drive 可以提供的东西来说太多了,我想就如何直接从 java.io.InputStream / OutputStream / Reader / Writer 读/写 Drive 而无需创建临时本地文件提出建议我要发送到驱动器的数据的副本。谢谢!

4

4 回答 4

0

这是我的应用程序中的一个(不完整)片段,可能会有所帮助。

            URL url = new URL(urlParam);
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("GET");
        connection
                .setRequestProperty("Authorization",
                        "OAuth "+accessToken);

        String docText = convertStreamToString(connection.getInputStream());
于 2012-08-27T16:09:26.807 回答
0

请查看 Google Drive SDK文档中提供的 DrEdit Java 示例。此示例说明如何授权和构建读取元数据、文件数据和将内容上传到 Google Drive 的请求。

这是一个代码片段,展示了如何使用ByteArrayContent将媒体上传到存储在字节数组中的 Google Drive:

/**
 * Create a new file given a JSON representation, and return the JSON
 * representation of the created file.
 */
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws IOException {
  Drive service = getDriveService(req, resp);
  ClientFile clientFile = new ClientFile(req.getReader());
  File file = clientFile.toFile();

  if (!clientFile.content.equals("")) {
    file = service.files().insert(file,
        ByteArrayContent.fromString(clientFile.mimeType, clientFile.content))
        .execute();
  } else {
    file = service.files().insert(file).execute();
  }

  resp.setContentType(JSON_MIMETYPE);
  resp.getWriter().print(new Gson().toJson(file.getId()).toString());
}
于 2012-08-24T17:47:34.333 回答
0

// 构建一个新的授权 API 客户端服务。驱动服务 = getDriveService();

    // Print the names and IDs for up to 10 files.
    FileList result = service.files().list()
         .setPageSize(10)
         .setFields("nextPageToken, files(id, name)")
         .execute();

    List<File> files = result.getFiles();
    if (files == null || files.size() == 0) {
        System.out.println("No files found.");
    } else {
        System.out.println("Files:");
        for (File file : files) {
            System.out.printf("%s (%s)\n", file.getName(), file.getId());
            String fileId = file.getId();

                Export s=service.files().export(fileId, "text/plain");
                InputStream in=s.executeMediaAsInputStream();
                InputStreamReader isr=new InputStreamReader(in);
                BufferedReader br = new BufferedReader(isr);
                String line = null;

                StringBuilder responseData = new StringBuilder();
                while((line = br.readLine()) != null) {
                    responseData.append(line);
                }
                System.out.println(responseData);
            } 
        }
    }
于 2017-10-18T09:02:51.277 回答
0

使用 google-api-services-drive-v3-rev24-java-1.22.0:

要读取文件的内容,请确保DriveScopes.DRIVE_READONLYGoogleAuthorizationCodeFlow.Builder(...)凭据授权方法/代码中进行设置。

您将需要fileId您要阅读的文件。你可以做这样的事情:然后
FileList result = driveService.files().list().execute();
你可以迭代你想要阅读的resultfilefileId

一旦你这样做了,阅读内容将是这样的:

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId).executeMediaAndDownloadTo(outputStream);
InputStream in = new ByteArrayInputStream(outputStream.toByteArray());
于 2016-05-14T13:25:49.467 回答