简体   繁体   中英

How to upload file to Google drive with java web application…?

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))
    .setAccessType("online")
    .setApprovalPrompt("auto").build();

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(System.in));
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");

java.io.File fileContent = new java.io.File("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( java.io.File fileContent = new java.io.File("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.

MyHtml.html

FirstName:
LastName:
Email:



The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM