使用java api将文件上载到google drive

使用java api将文件上载到google drive,java,google-drive-api,Java,Google Drive Api,使用此代码我上传文件,但该文件是作为文档上传的。 意思是说我上传任何类型的文件都是作为文档上传到google drive中的据我所知,从我查看到现在已经有几个月了,这还不是吗?由谷歌驱动器API支持。 作为解决方案,请考虑安装本地谷歌驱动器共享,并将要上载的文件写入本地映射的共享文件夹。然后它的谷歌驱动问题来处理上传。可以指导你 DocsService client = new DocsService("service name"); client.setUserCredentials(user

使用此代码我上传文件,但该文件是作为文档上传的。
意思是说我上传任何类型的文件都是作为文档上传到google drive中的

据我所知,从我查看到现在已经有几个月了,这还不是吗?由谷歌驱动器API支持。
作为解决方案,请考虑安装本地谷歌驱动器共享,并将要上载的文件写入本地映射的共享文件夹。然后它的谷歌驱动问题来处理上传。

可以指导你

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);
对于servlet 保存到驱动器的函数

$("#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");
   }

}
});    
});

我知道这个问题已经很老了,但现在google已经有了官方的支持,有一个快速的示例可以开始将驱动API与java应用程序集成。 从下载驱动器API客户端jar文件


如果您是从Java SE开发应用程序,请不要忘记将servlet api.jar放在类路径上,否则您会出现很多错误。

hello@Shersha Fn我是这方面的新手,我想从头开始编写Java代码,提供一个按钮,将文件上传到我的google drive帐户中!我该怎么做?我没有得到任何关于这方面的线索,文档正在我的头上
$("#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");
   }

}
});    
});
 //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;
    }

  }