2013-04-26 144 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

相关问题