0
votes

I am trying to have the program upload all files in a designated filepath on a local directory onto Google Drive. Having accomplished the opposite with download all the files in a folder, I thought that I would stick with the same methodology of listing all the files in local directory first, then uploading them one by one as each file is listed.

This code along lists all the names of the files in a designated filepath

private static File uploadFile(Drive service, String originfolder) {
        java.io.File dir = new java.io.File(originfolder);
        String [] fileslist = dir.list();
        for (String file : fileslist) {
            System.out.println(file);

I know that the code to upload a single file is as below

File fileMetadata = new File();
fileMetadata.setName("photo.jpg");
java.io.File filePath = new java.io.File("files/photo.jpg");
FileContent mediaContent = new FileContent("image/jpeg", filePath);
File file = driveService.files().create(fileMetadata, mediaContent)
    .setFields("id")
    .execute();
System.out.println("File ID: " + file.getId());

as seen from here

Trying to combine the two results in the following snippet:

private static File uploadFile(Drive service, String originfolder) {
        java.io.File dir = new java.io.File(originfolder);
        String [] fileslist = dir.list();
        for (String file : fileslist) {
            System.out.println(file);
            File uploadfile = new File();
            uploadfile.setName(originfolder);
            FileContent mediaContent = new FileContent("image/jpeg", originfolder);
            File uploadedfile = service.files().create(uploadfile, mediaContent)
                .setFields("id")
                .execute();
            System.out.println("File ID: " + file);
        }
    }

The error I get from cmd is this

 error: incompatible types: String cannot be converted to File
                        FileContent mediaContent = new FileContent("image/jpeg", originfolder);

The planned intent is to upload the entire content of the following file hierarchy onto Google Drive, whilst keeping the same file names and mimetype.

Logs (folder).

--- bin (folder)

------ approx 1 GB of .bin files

--- 2 xml files

1
Is it because service.files().create(uploadfile, mediaContent) .setFields("id") .execute() returns a String and not a file?Joe
Not exactly. String originfolder and String [] fileslist = dir.list(); are all type String. They need to somehow be type File, to be compatible with the upload code. At least that's what I think. Not completely surePhanotak

1 Answers

1
votes

Solved it

For anyone who wants to know, code ended up looking like this

private static void uploadFile(Drive service, String originfolder) {
        try {
            java.io.File dir = new java.io.File(originfolder);
            String [] fileslist = dir.list();
            for (String file : fileslist) {
                System.out.println(file);
                File uploadfile = new File();
                uploadfile.setName(file);
                java.io.File filePath = new java.io.File(originfolder + file);
                FileContent mediaContent = new FileContent("image/jpeg", filePath);
                File uploadedfile = service.files().create(uploadfile, mediaContent)
                    .setFields("id")
                    .execute();
                System.out.println("File ID: " + file);
            }
        } catch (IOException e) {
            System.out.println("An error occurred: " + e);
        }
    }