【问题标题】:Fail to upload a image file into Google Doc via java api无法通过 java api 将图像文件上传到 Google Doc
【发布时间】:2023-03-27 02:55:01
【问题描述】:

下面是我的代码

DocsService client = new DocsService("testappv1");
client.setUserCredentials(username, password);
client.setProtocolVersion(DocsService.Versions.V2);
File file = new File("C:/test.jpg");
DocumentEntry newDocument = new DocumentEntry();
newDocument.setTitle(new PlainTextConstruct("test"));
String mimeType = DocumentListEntry.MediaType.fromFileName(file.getName()).getMimeType();
newDocument.setMediaSource(new MediaFileSource(file, mimeType));
newDocument = client.insert(destFolderUrl, newDocument);

文档创建成功,但它不包含任何内容。

【问题讨论】:

  • 我假设它是一个空的文本文档,那是创建的吗?你想要这个吗?还是您希望上传的文件是纯图像?
  • 我想创建一个只包含图像的 google-doc 格式文档。就像我直接将图像文件拖到 google-doc 中一样。

标签: java google-docs google-docs-api


【解决方案1】:

试试下面的

client.insert(new URL("https://docs.google.com/feeds/documents/private/full/?convert=false"), newDocument);

我认为?convert=false 位很重要,不知道如果没有 url 你是怎么做到的

client.insert(new URL(destFolderUrl+ "?convert=false"), newDocument);

希望对你有用

【讨论】:

  • 在我的谷歌文档设置中,我设置了将文档转换为谷歌格式。如果我在我的 URL 后面添加“convert=false”,它会抛出一个错误。 URL destFolderUrl = new URL("https://docs.google.com/feeds/folders/private/full/folderResourceId?convert=false");
  • 我认为那是你的问题,它试图在不知道如何转换 jpeg 时尝试转换,你不应该这样做。
  • 所以有什么建议吗?它工作正常,最近才遇到这个问题。
  • 这个问题似乎与here 类似。您可以尝试使用提供的代码 sn-p 上传您的文件吗?
  • 嗨 Alain,感谢您的重播,实际上我希望 google 转换我的图像,因为我以后需要通过文档列表 api 将其删除。我还没有尝试过那个代码snippert。我确实希望它有效,但也希望它能够简单地完成,就像上传一个 word 格式文件一样。
猜你喜欢
  • 1970-01-01
  • 2018-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-22
相关资源
最近更新 更多