0
如何使用java將文件上傳到谷歌驅動器。 我的java代碼是。如何使用java web應用程序將文件上傳到Google驅動器......?
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());
}}
所以在該文件中的對象(的java.io.File fileContent =新的java.io.File(「文檔.txt」))要求完整的文件path.But在文件上傳,我們只能得到文件名不是路徑。如何解決問題。請幫助我。
我不太清楚你想在這個問題上有什麼。但是如果你想獲得文件名而不是文件路徑。 java.io.File.getName()方法返回路徑名名稱序列的最後一個名稱,這意味着文件的名稱。 – gui47 2014-12-04 17:58:41
好吧,如果我通過**文件內容中的文件名稱**它是說filNotFoundException.That意味着它想要確切的路徑文件所在的位置。但從Web應用程序,你知道我們只能得到文件名。解決這個問題。 – kamesh 2014-12-05 05:07:07
@kamesh,你是如何獲得文件名第一? – Sridhar 2014-12-10 13:40:59