我想阅读和写一个文本文档,我在java中的“一个驱动器”或“谷歌驱动器”上传。
BufferedReader bufferedReader;
String line;
URL url = new URL("https://docs.google.com/document/d/1JLRmW7eaXHyyoPdNCyNN7AHWzKWaAPWgL84sxHUB7dQ/edit");
bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
使用此代码我只阅读HTML文档,这没有用。在本文中是我必须与客户端连接的服务器的一些IP。请注意,我想从java程序访问此文件,而不是Android应用程序。正如我研究的那样,可用的google驱动器api不太适合java实现,因为它们是为Android解决方案构建的。
可用的google驱动器api不太适合java实现
Google API支持Java。 Android是Java,因此API(需要调用的类,方法等)在任一平台上都是相同的。
Drive API是Google API客户端库的一部分。这是一个很好的概述:
一个Developer's Guide有关于设置和一些Examples的说明。
drive-cmdline-sample项目将是一个很好的起点。有一个downloadFile方法,看起来会得到源文件:
/** Downloads a file using either resumable or direct media download. */
private static void downloadFile(boolean useDirectDownload, File uploadedFile)
throws IOException {
// create parent directory (if necessary)
java.io.File parentDir = new java.io.File(DIR_FOR_DOWNLOADS);
if (!parentDir.exists() && !parentDir.mkdirs()) {
throw new IOException("Unable to create parent directory");
}
OutputStream out = new FileOutputStream(new java.io.File(parentDir, uploadedFile.getTitle()));
MediaHttpDownloader downloader =
new MediaHttpDownloader(httpTransport, drive.getRequestFactory().getInitializer());
downloader.setDirectDownloadEnabled(useDirectDownload);
downloader.setProgressListener(new FileDownloadProgressListener());
downloader.download(new GenericUrl(uploadedFile.getDownloadUrl()), out);
}