upload file into google drive using java api

前端 未结 3 926
后悔当初
后悔当初 2021-01-16 17:50
DocsService client = new DocsService(\"service name\");
client.setUserCredentials(username,password);
File file = new File(filename);
URL url = new URL(\"https://doc         


        
3条回答
  •  感动是毒
    2021-01-16 18:04

    can guide you with this

     
    
    

    //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 servlet here function to save to drive

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

提交回复
热议问题