1
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

3 Answers 3

3

As far as I know (its been a few month since I looked) this is not (yet?) supported by the google drive 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. Then its google drives problem to handle the upload.

Sign up to request clarification or add additional context in comments.

Comments

2

can guide you with this

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

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

  }

Comments

2

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. Download drive API client jar files from here

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.

1 Comment

hello @Shersha Fn I am beginner in this cloud thing and i want to write a java code from scratch to provide a button to upload files into my google drive account ! how can i do that ? I am not getting any thread on this and documentation is going over my head

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.