【问题标题】:How to upload file to Google drive with java web application...?如何使用 Java Web 应用程序将文件上传到 Google 驱动器...?
【发布时间】:2014-12-04 06:17:55
【问题描述】:

如何使用 java 将文件上传到谷歌驱动器。 我的java代码是。

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 = new java.io.File("document.txt");) 询问完整的文件路径。但是在文件上传中我们可以得到只有文件名不是路径。如何解决这个问题。请帮助我。

【问题讨论】:

  • 我不太确定你想在这个问题上做什么。但是如果你想获取文件名而不是文件路径。 java.io.File.getName() 方法返回路径名的名称序列的最后一个名称,即文件名。
  • 好吧,如果我在 file-Content 中传递文件名,那就是 filNotFoundException。这意味着它需要文件所在的确切路径。但是从 Web 应用程序中你知道我们只能获取文件名。那么如何解决这个问题。
  • @kamesh,你是如何获得文件名的?
  • 嗨,sridhar,我正在通过网络应用程序将文件上传到 Google 驱动器。所以在这个过程中,我们只能从请求对象中获取文件名。所以在这种情况下,它询问的是绝对路径。我如何提供文件路径。有什么方法可以使用带有 java 的 Web 应用程序将文件上传到 Google 驱动器...?

标签: java google-drive-api


【解决方案1】:

MyHtml.html

名字:
姓氏:
电子邮件:



【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    相关资源
    最近更新 更多