更新时间:2023-02-14 14:23:59
You are using a ByteArrayOutputStream
object as the output of your download. If your program terminates without having saved the contents of this object somewhere, you will not be able to find this information in your computer's disk, as it is not written to it but rather saved in memory as a buffered byte-array (refer to the previous link for more information).
If you want to save the output of the download to the file, I suggest you use instead a FileOutputStream
as the destination of your download. In order to do that, you have to modify your code as follows:
添加适当的import
声明:
import java.io.FileOutputStream;
修改您的outputStream
变量分配,如下所示:
Modify your outputStream
variable assignment as follows:
OutputStream outputStream = new FileOutputStream('/tmp/downloadedfile');
传递给FileOutputStream
的参数应该是下载所需的目标路径.
Where the parameter passed to FileOutputStream
should be the desired destination path of your download.
outputStream.flush();
outputStream.close();
这样可以确保文件被正确写入.
This will ensure that your file is being written to properly.
关于下载文件夹,您完全正确-首先需要获取要下载的文件夹及其每个子级.为了更好地理解操作方法,建议您检查以下答案:下载Google Drive API的文件夹
In regards to downloading a folder, you are completely right - you will first need to fetch the folder you want to download, and each of their children. In order to better understand how to do it, I suggest you check out the following answer: Download folder with Google Drive API
String destinationFolder = "/tmp/downloadedfiles/";
List<File> files = result.getFiles();
File newFile;
if (files == null || files.isEmpty()) {
System.out.println("No files found.");
} else {
System.out.println("Files:");
for (File file : files) {
System.out.printf("%s (%s)\n", file.getName(), file.getId());
String fileId = file.getId();
String fileName = file.getName();
OutputStream outputstream = new FileOutputStream(destinationFolder + fileName);
service.files().get(fileId)
.executeMediaAndDownloadTo(outputstream);
outputstream.flush();
outputstream.close();
}
}