无法通过java api将嵌入到HTML中的本地图像文件作为数据URI上传到google doc

时间:2012-04-30 07:54:44

标签: google-docs-api google-drive-api

我需要从本地磁盘上传图像文件。我将图像转换为base64格式,并将html文件写入本地磁盘,即可在浏览器中打开html文件并显示图像,但google doc中的文档只是一个空文件,即使我将该html文件拖入google docs图像仍然不存在。我的代码如下:

DocsService client = new DocsService("testappv3");
client.setUserCredentials("username", "password");
File file = new File("c:/test.bmp");
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));

int read;
byte[] buff = new byte[1024];
while ((read = in.read(buff)) > 0)
{
    out.write(buff, 0, read);
}
out.flush();

String base64 = Base64.encode(out.toByteArray());
String mimeType = DocumentListEntry.MediaType.fromFileName(file.getName()).getMimeType();
String html = "<html><body><img src=\"data:" + mimeType + ";base64," + base64 + "\"/></body></html>";


URL destFolderUrl = new URL("https://docs.google.com/feeds/default/private/full/<FOLDER_ID>/contents");
DocumentEntry newDocument = new DocumentEntry();
newDocument.setTitle(new PlainTextConstruct("test"));
newDocument.setMediaSource(new MediaByteArraySource(html.getBytes(), "text/html"));
newDocument = client.insert(destFolderUrl, newDocument);

1 个答案:

答案 0 :(得分:0)

这看起来像一个错误,我们正在尝试修复它,当我们修复时我会回复你。

感谢您的耐心等待。

相关问题