Java中的Google Drive API - 404文件存在时

时间:2017-04-09 16:00:13

标签: java download google-drive-api

美好的一天

在使用FileID从Google云端硬盘下载文件时,我遇到了一些问题。我已经到处寻找答案,但我找不到适用于我程序的答案。

我一直在使用两段代码:一段来自Google Developers网站here,另一段来自Google GitHub的样本。到目前为止,我无法使用FileID从我的云端硬盘下载文件。

我的代码如下:

public class DownloadTask extends SwingWorker<Void, Void> {

private String downloadURL;
private UpdateGUI gui;
private final String APPLICATION_NAME = "PROGRAM";
private final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"), ".store/program");
private FileDataStoreFactory dataStoreFactory;
private HttpTransport httpTransport;
private final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private Drive drive;

public DownloadTask(String downloadURL, UpdateGUI gui) {
    this.downloadURL = downloadURL;
    this.gui = gui;
}

private Credential authorize() throws Exception {
    // load client secrets
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            new InputStreamReader(this.getClass().getResourceAsStream("/client_secrets.json")));
    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            httpTransport, JSON_FACTORY, clientSecrets,
            Collections.singleton(DriveScopes.DRIVE)).setDataStoreFactory(dataStoreFactory)
            .build();
    // authorize
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}

/**
 * Executed in background thread
 */
@Override
protected Void doInBackground() throws Exception {
    try {
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
        // authorization
        Credential credential = authorize();
        // set up the global Drive instance
        drive = new Drive.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(
                APPLICATION_NAME).build();

        // run commands
        downloadFile(false, downloadURL);
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "Error downloading file: " + ex.getMessage(),
                "Error", JOptionPane.ERROR_MESSAGE);
        cancel(true);
        System.exit(1);
    }
    return null;
}

private void downloadFile(boolean useDirectDownload, String fileLocation)
        throws IOException {
    OutputStream out = new FileOutputStream(new java.io.File("DownloadedFile.jar"));

    GenericUrl url = drive.files().get(fileLocation).buildHttpRequestUrl();

    MediaHttpDownloader downloader
            = new MediaHttpDownloader(httpTransport, drive.getRequestFactory().getInitializer());
    downloader.setDirectDownloadEnabled(useDirectDownload);
    downloader.setProgressListener(gui);
    downloader.download(url, out);
}

@Override
protected void done() {
    //extra code
}
}

downloadURL变量是FileID,我从数据库中读取。我正在下载的文件是一个JAR文件(可执行文件)。

如果我需要提供任何其他信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

尝试使用Download Files文档

中的代码

使用Drive API客户端库。

String fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId)
        .executeMediaAndDownloadTo(outputStream);

另请尝试检查SO post以检查exportLink(在v2中可用),但在v3中尝试使用Files: export

注意:确保Drive API中存在文件ID。错误的文件ID可能导致错误404。

希望这有帮助。