2013-04-26 136 views
0
DocsService client = new DocsService("service name"); 
client.setUserCredentials(username,password); 
File file = new File(filename); 
URL url = new URL("https://docs.google.com/feeds/default/private/full/?ocr=true&convert=true"); 
String mimeType =  DocumentListEntry.MediaType.fromFileName(file.getName()).getMimeType(); 
DocumentEntry newDocument = new DocumentEntry(); 
newDocument.setTitle(new PlainTextConstruct(file.getName())); 
newDocument.setMediaSource(new MediaFileSource(file, mimeType)); 
//newDocument.setFile(file, mimeType); 
newDocument = client.insert(url, newDocument); 
System.out.println("uploaded "+file.getName()); 
JOptionPane.showMessageDialog(null, "uploaded "+file.getName(), "alert", JOptionPane.ERROR_MESSAGE); 

使用此代碼我上傳文件,但該文件上傳爲文檔。 意味着我上傳任何類型的文件被上傳的文檔中的谷歌驅動使用java api上傳文件到谷歌驅動器

回答

3

據我所知,(它是一個幾個月,因爲我看了),這是(尚?)由谷歌支持驅動API。 作爲解決方法,請考慮安裝本地谷歌驅動器共享並將要上載的文件寫入本地映射的共享文件夾。然後,它的谷歌驅動器問題來處理上傳。

2

可以指導您完成此

<input id="file-pdf" type="file" name="file-pdf"> 
<button id="submit-pdf">submit</button> 

// JavaScript的

$("#submit-pdf").click(function() { 
var inputFileImage = document.getElementById("file-pdf"); 
var file = inputFileImage.files[0]; 
var data = new FormData(); 
data.append("file-pdf",file); 
$.ajax({ 
url: "uploadpdf", 
type: 'POST', 
cache : false, 
data : data, 
processData : false, 
contentType : false, 
dataType: "json",  
success: function (response) {   
    if(response.success){ 
     console.log("ok"); 
    }else{ 
     console.log("fail"); 
    } 

} 
});  
}); 

對servlet的here 功能,節省駕駛

//parentId ID folder drive 
public static File insertFile(GoogleCredential credential,String title, String parentId, String mimeType, String filename, InputStream stream) { 

    try { 
      Drive driveService = new Drive.Builder(httpTransport, jsonFactory, null).setApplicationName("DRIVE_TEST").setHttpRequestInitializer(credential).build(); 

      // File's metadata. 
      File body = new File(); 
      body.setTitle(title); 
      body.setMimeType(mimeType); 

      // Set the parent folder. 
      if (parentId != null && parentId.length() > 0) { 
       body.setParents(
        Arrays.asList(new ParentReference().setId(parentId))); 
      } 

      // File's content. 
      InputStreamContent mediaContent = new InputStreamContent(mimeType, new BufferedInputStream(stream)); 
      try { 
       File file = driveService.files().insert(body, mediaContent).execute(); 

       return file; 
      } catch (IOException e) { 
       logger.log(Level.WARNING, "un error en drive service: "+ e); 
       return null; 
      } 

    } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
      return null; 
    } 

    } 
2

我知道這個問題是很老,但現在谷歌官方支持Java drive api,有一個快速示例來開始將Drive API與Jav​​a應用程序集成。從here

下載驅動API客戶端jar文件,如果你正在開發從Java SE應用程序中不要忘了把servlet的api.jar文件類路徑上,否則你將結束與很多的誤區。

+0

hello @Shersha Fn我是這個雲端事物的初學者,我想從頭開始編寫一個java代碼,以提供一個按鈕,將文件上傳到我的谷歌帳號!我怎樣才能做到這一點 ?我沒有得到任何線索,文件正在我的頭上 – minigeek 2017-02-13 10:01:07

相關問題