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