我需要從本地磁盤上傳圖像文件。我將圖像轉換爲base64格式,並將html文件寫入本地磁盤,以便可以在瀏覽器中打開html文件並顯示圖像,但文檔在google doc中只是一個空文件,即使我將該文件拖到Google docs圖像仍然不存在。我的代碼如下:無法上傳本地圖像文件嵌入到HTML作爲數據URI到谷歌文檔通過java api
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);
爲什麼你上傳一個帶有text/html的圖像作爲內容類型? –
beacse我將這個圖像文件嵌入到一個html文件中,然後將html文件上傳到google文檔中。行圖片文件無法上傳到谷歌文檔 – user1335794