[英]Uploading All Files from local directory to Google Drive (Google Drive API V3)
I am trying to have the program upload all files in a designated filepath on a local directory onto Google Drive.我试图让程序将本地目录上指定文件路径中的所有文件上传到 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());
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我从 cmd 得到的错误是这个
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.计划的目的是将以下文件层次结构的全部内容上传到 Google Drive,同时保持相同的文件名和 mimetype。
Logs (folder).日志(文件夹)。
--- bin (folder) --- bin(文件夹)
------ approx 1 GB of .bin files ------ 大约 1 GB 的 .bin 文件
--- 2 xml files --- 2个xml文件
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.