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