Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
    Bookmark Topic Watch Topic
  • New Topic

How to upload file to Google drive with java web application?  RSS feed

Posts: 12
  • Mark post as helpful
  • send pies
  • Report post to moderator
How to upload file to google drive using java. my java code is.

HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE))

String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();
System.out.println("Please open the following URL in your browser then type the authorization code:");
System.out.println(" " + url);
BufferedReader br = new BufferedReader(new InputStreamReader(;
String code = br.readLine();

GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response);

//Create a new authorized API client
Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).build();

//Insert a file
File body = new File();
body.setTitle("My document");
body.setDescription("A test document");
body.setMimeType("text/plain"); fileContent = new"document.txt");
FileContent mediaContent = new FileContent("text/plain", fileContent);

File file = service.files().insert(body, mediaContent).execute();
System.out.println("File ID: " + file.getId());

So in that file object( fileContent = new"document.txt");) asking complete file path.But in file upload we can get only file name not path.How to resolve the problem.please help me.
Posts: 42975
  • Mark post as helpful
  • send pies
  • Report post to moderator
That seems to be the same question as (and it has nothing to do with uploading to Google Drive, but with uploading to a Java web app), so let's continue the conversation over there - you got a couple of good answers there that you didn't respond to.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!