This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DocumentListEntry uploadedEntry = uploadFile("/path/to/your/file.doc", "TitleToUse"); | |
System.out.println("Document now online @ :" + uploadedEntry.getHtmlLink().getHref()); | |
public DocumentListEntry uploadFile(String filepath, String title) | |
throws IOException, ServiceException { | |
File file = new File(filepath); | |
DocumentListEntry newDocument = new DocumentListEntry(); | |
String mimeType = DocumentListEntry.MediaType.fromFileName(file.getName()).getMimeType(); | |
newDocument.setFile(new File(filepath), mimeType); | |
newDocument.setTitle(new PlainTextConstruct(title)); | |
return client.insert(new URL("https://docs.google.com/feeds/documents/private/full/"), newDocument); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gdata.client.docs.DocsService; | |
import com.google.gdata.data.MediaContent; | |
import com.google.gdata.data.PlainTextConstruct; | |
import com.google.gdata.data.docs.DocumentListEntry; | |
import com.google.gdata.data.docs.DocumentListEntry.MediaType; | |
import com.google.gdata.data.media.MediaByteArraySource; | |
import com.google.gdata.data.media.MediaSource; | |
import com.google.gdata.util.ContentType; | |
// ...skipping to the good part... | |
DocsService docsService = new DocsService("myname-webapp-1.0"); | |
// Authenticate our direct user access (using direct login credentials) | |
docsService.setUserCredentials(MY_USER_NAME, MY_PASSWORD); | |
DocumentListEntry newDocument = new DocumentListEntry(); | |
// Load the byte array into a MediaSource | |
MediaByteArraySource mediaSource = new MediaByteArraySource(byteData, MediaType.fromFileName(myFileName).getMimeType()); | |
MediaContent content = new MediaContent(); | |
content.setMediaSource(mediaSource); | |
content.setMimeType(new ContentType(mediaSource.getContentType())); | |
newDocument.setContent(content); | |
String gdocsFilename = new String("My Filename"); | |
newDocument.setTitle(new PlainTextConstruct(gdocsFilename)); | |
// Push it into Google Docs!! | |
DocumentListEntry uploadedRef = docsService.insert(new URL("https://docs.google.com/feeds/default/private/full/"), newDocument); |
No comments:
Post a Comment