简体   繁体   中英

Using String[] args Parameter as Variable in .setQ Parameters (Google Drive API V3)

I'm trying to download .bin and .xml files from Google Drive using the API, so I created a parameter in the String[] args that defines the mime type.

The method requires specifying the mimetype in the .setQ, so I'm wondering if mimeType can be set to the parameter that defines whether its .bin or .xml.

private static void downloadFile(Drive service, String src_dir, String dst_dir, String mime_type) {
        try {
            FileList result = service.files().list()
                //application/x-binary for bin files; (application or text)/xml for xml files
                .setQ("'googledrivefolderid' in parents " + "and (mimeType = mime_type)")
                .setFields("nextPageToken, files(id, name)")
                .execute();
            List<File> files = result.getFiles();
            if (files == null || files.isEmpty()) {
                System.out.println("No files found.");
            } else {
                //System.out.println("Files:");
                for (File file : files) {
                    String fileId = file.getId();
                    String fileName = file.getName();
                    OutputStream outputstream = new FileOutputStream(dst_dir + fileName);
                    service.files().get(fileId)
                    .executeMediaAndDownloadTo(outputstream);
                    outputstream.flush();
                    outputstream.close();
                }
            }
        } catch (IOException e) {
            System.out.println("An error occurred: " + e);
        }
    }
  • You want to retrieve the files with the mimeTypes of .bin or .xml in the specific folder using files.list method in Drive API v3.
    • The mimeTypes of the files of .bin or .xml are application/x-binary and (application or text)/xml , respectively.
  • You want to achieve this using the search query.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Sample search query 1:

The search query for retrieving the files of application/x-binary or application/xml or text/xml in the specific folder of googledrivefolderid can be created as follows.

'googledrivefolderid' in parents and (mimeType = 'application/x-binary' or mimeType = 'application/xml' or mimeType = 'text/xml')
  • If the mimeType of the files are different, you can check it using the method of Files: get in Drive API. You can test it at here . When you test it, please input the file ID and click "EXECUTE" button.

References:

If I misunderstood your question and this was not the direction you want, I apologize.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM