使用来自Java Web应用程序的Google Docs API(Gdata)上传文档

时间:2012-05-31 20:17:52

标签: java gdata-api

我正在开发一个基于网络的应用程序,该应用程序允许用户使用GData Java API将Word文档上传到Google Docs。

(我遇到了this博客,我发现我实际上可以使用字节数组来上传文档,而不是使用文件

我正在使用Netbeans + JDK 1.6

我的servlet中的相关代码:

DocsService docsService = new DocsService("care.udhc.co.in");                
try {
    docsService.setUserCredentials("sbose78@gmail.com", "*******");      

    DocumentListEntry newDocument = new DocumentListEntry();

    String s="hello bose";
    byte byteData[]=s.getBytes();

    // Load the byte array into a MediaSource
    MediaByteArraySource mediaSource = new MediaByteArraySource(byteData, MediaType.fromFileName("bose.doc").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));
    out.println("OK");
    // Push it into Google Docs!! 
    DocumentListEntry uploadedRef = docsService.insert(new URL("https://docs.google.com/feeds/default/private/full/"), newDocument);
} catch(Exception e) {
   out.println(e.toString());
} finally {            
    out.close();
}

当我运行本地时,我遇到以下错误:

com.google.gdata.util.InvalidEntryException: We're sorry, a server error occurred. Please try again. GDataInvalidEntryExceptionWe're sorry, a server error occurred. Please try again.

当我运行部署在互联网上的版本(Jelastic cloud)时, 我明白了:

java.lang.NoClassDefFoundError: com/google/gdata/data/extensions/QuotaBytesTotal
com.google.gdata.data.docs.MetadataEntry.declareExtensions(MetadataEntry.java:86)
com.google.gdata.data.ExtensionProfile.addDeclarations(ExtensionProfile.java:71)
com.google.gdata.data.BaseFeed.declareExtensions(BaseFeed.java:235)
com.google.gdata.client.docs.DocsService.declareExtensions(DocsService.java:171)
com.google.gdata.client.docs.DocsService.<init>(DocsService.java:108)
bose.google.UploadToDocs.processRequest(UploadToDocs.java:30)
bose.google.UploadToDocs.doGet(UploadToDocs.java:79)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

请给我一个解决方法?

1 个答案:

答案 0 :(得分:0)

好像你缺少一个必需的依赖项,可能是gdata-core-1.0.jar。

另外,请检查此页面是否存在外部依赖关系:https://developers.google.com/gdata/articles/java_client_lib