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

文件存在时Java-404中的Google驱动API,java,download,google-drive-api,Java,Download,Google Drive Api,大家好 当使用FileID从googledrive下载文件时,我遇到了一点问题。我到处寻找答案,但我找不到一个与我的程序相符的答案 我一直在使用两段代码:一段来自Google开发者网站,另一段来自位于的Google API示例。到目前为止,我无法使用FileID从驱动器下载文件 我的代码如下: public class DownloadTask extends SwingWorker<Void, Void> { private String downloadURL; private

大家好

当使用FileID从googledrive下载文件时,我遇到了一点问题。我到处寻找答案,但我找不到一个与我的程序相符的答案

我一直在使用两段代码:一段来自Google开发者网站,另一段来自位于的Google API示例。到目前为止,我无法使用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
}
}
公共类下载任务扩展SwingWorker{
私有字符串下载URL;
私有更新gui;
专用最终字符串应用程序\u NAME=“程序”;
private final java.io.File DATA_STORE_DIR=new java.io.File(System.getProperty(“user.home”),“.STORE/program”);
私有文件数据存储工厂数据存储工厂;
私人HttpTransport HttpTransport;
私有最终JsonFactory JSON_FACTORY=JacksonFactory.getDefaultInstance();
私家车;
公共下载任务(字符串下载URL、更新gui){
this.downloadURL=下载URL;
this.gui=gui;
}
私有凭据授权()引发异常{
//加载客户端机密
GoogleClientSecrets clientSecrets=GoogleClientSecrets.load(JSON_工厂,
新的InputStreamReader(this.getClass().getResourceAsStream(“/client_secrets.json”);
//设置授权代码流
GoogleAuthorizationCodeFlow=新建GoogleAuthorizationCodeFlow.Builder(
httpTransport、JSON_工厂、客户机密、,
Collections.singleton(DriveScopes.DRIVE)).setDataStoreFactory(dataStoreFactory)
.build();
//授权
返回新的AuthorizationCodeInstalledApp(流,新的LocalServerReceiver())。授权(“用户”);
}
/**
*在后台线程中执行
*/
@凌驾
受保护的Void doInBackground()引发异常{
试一试{
httpTransport=GoogleNetHttpTransport.newTrustedTransport();
dataStoreFactory=新文件dataStoreFactory(DATA\u STORE\u DIR);
//授权书
凭证=授权();
//设置全局驱动器实例
drive=new drive.Builder(httpTransport,JSON_工厂,凭证)。setApplicationName(
应用程序名称).build();
//运行命令
下载文件(假,下载URL);
}捕获(IOEX异常){
JOptionPane.showMessageDialog(null,“下载文件时出错:”+ex.getMessage(),
“错误”,作业窗格。错误消息);
取消(真);
系统出口(1);
}
返回null;
}
私有void下载文件(布尔useDirectDownload,字符串文件位置)
抛出IOException{
OutputStream out=新文件OutputStream(新的java.io.File(“downloaddedfile.jar”);
GenericUrl url=drive.files().get(fileLocation.buildHttpRequestUrl();
MediaHttpDownloader下载程序
=新的MediaHttpDownloader(httpTransport,drive.getRequestFactory().getInitializer());
downloader.setDirectDownloadeEnabled(使用DirectDownload);
setProgressListener(gui);
下载(url,out);
}
@凌驾
受保护的void done(){
//额外代码
}
}
downloadURL
变量是我从数据库读取的文件ID。我正在下载的文件是一个JAR文件(可执行文件)


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

尝试使用文档中的代码

使用驱动器API客户端库。 另外,请尝试检查以便检查exportLink(在v2中可用),但在v3中,请尝试使用

注意:确保驱动器API中存在文件ID。不正确的文件ID可能导致错误404

希望这有帮助

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