简体   繁体   English

使用java api将文件上传到谷歌驱动器

[英]upload file into google drive using java 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);

using this code i upload file but that file is upload as a document.使用此代码我上传文件,但该文件作为文档上传。 means i upload any type of that file is upload as a document in to google drive意味着我上传任何类型的文件作为文档上传到谷歌驱动器

As far as I know (its been a few month since I looked) this is not (yet?) supported by the google drive API.据我所知(自从我看了几个月以来)这不是(还?)谷歌驱动器 API支持的。 As a workaround consider installing the native google drive share and write the files you want to upload into the locally mapped shared folder.作为一种解决方法,请考虑安装本机 google 驱动器共享并将要上传的文件写入本地映射的共享文件夹。 Then its google drives problem to handle the upload.然后它的谷歌驱动器问题来处理上传。

can guide you with this可以指导你这个

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

//javascripts //javascripts

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

}
});    
});

for servlethere function to save to drive servlethere函数保存到驱动器

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

  }

I know this question is quite old ,but now google has official support for Java drive api ,there is a quick sample to start integrating Drive API with java application.我知道这个问题已经很老了,但现在谷歌官方支持Java drive api ,有一个快速示例可以开始将 Drive API 与 java 应用程序集成。 Download drive API client jar files from here这里下载驱动 API 客户端 jar 文件

and if you are developing your application from Java SE don't forget to put servlet api.jar on class path or else you will end up with lot of errors.如果您正在从 Java SE 开发您的应用程序,请不要忘记将 servlet api.jar 放在类路径上,否则您最终会遇到很多错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Java Google Drive API 上传文件 - How to upload file using Java Google Drive API 使用 Java 和 Google Drive API V3 将文件上传到共享的 Google Drive 位置? - Upload a file to shared google drive location using Java with Google Drive API V3? 使用Google Drive Api上传文件 - Upload file using Google Drive Api Java Google Drive Android API,上传后获取文件ID - Java Google Drive Android API, get file ID after upload 使用java将文件从谷歌应用引擎上传到谷歌驱动器 - Upload file from google app engine to google drive using java 如何使用 Java Google Drive API 创建文件夹动态和上传多个文件 - How to create folder dynamic and upload multiples file using Java Google Drive API 如何使用drive api java从谷歌驱动器下载文件? - How to download a file from google drive using drive api java? 如何使用Java API在Google驱动器中上传Excel工作表 - how to upload an excel sheet in google drive using java api 无法使用 REST API 在谷歌驱动器的特定文件夹中上传文件 - Unable to upload file in specific folder in google drive using REST API 使用 API 将文件上传到 Google Drive 并出现错误 403 - Upload the file to Google drive using API and error 403
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM