2014-03-31 25 views

回答

0

创建从要为内容的字符串ByteArrayInputStream,然后通过一个InputStreamContent对象驱动API的insert方法:

File body = new File(); 
body.setTitle("My document"); 
body.setDescription("A test document"); 
body.setMimeType("text/plain"); 

final String textContent = "Hi! my first google doc!"; 
ByteArrayInputStream bis = new ByteArrayInputStream(textContent.getBytes("UTF-8")); 
InputStreamContent content = new InputStreamContent(); 
content.inputStream = bis; 
Drive.files.insert(body, content); 
+0

嗨spinlok,非常感谢你。它的工作。 – user1012283

+0

嗨spinlok,如何在textContent中添加转义字符,如',\,甚至是新行? – user1012283

+0

您必须使用通常的字符串转义机制来转义它们:请参阅http://docs.oracle.com/javase/tutorial/java/data/characters.html。举个例子,如果你想要一个文字'\',你需要使用'\\'来表示字符串 – spinlok