问题描述
使用 Google Drive API 时,我在这里使用 downloadMetadataFile() 来处理文件:
public void downloadMetadataFile(String fileId,String folderStorePath,String fileName) throws IOException,GeneralSecurityException,GoogleException {
String path = folderStorePath + "/" + fileName
java.io.File file = new java.io.File(path);
try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
Drive drive = createDrive();
drive.files().get(fileId)
.executeMediaAndDownloadTo(fileOutputStream);
}
}
当使用上述方法且文件夹存在时(izakayaTemplate +居酒屋):
- 当path=/reports/template/izakayaTemplate/template3.png时,方法工作文件和从Google Drive下载template3.png成功
- 当path=/reports/template/居酒屋/template3.png时,该方法在第
try (FileOutputStream fileOutputStream = new FileOutputStream(file))
行抛出FileNotFoundException
有人可以为我解释一下这种行为吗?
注意:
- 我使用的是 SpringBoot 2.5、Java 8、Drive API v3
- 我正在 Amazon linux 1 上通过 DaemonTool 作为服务运行这个项目。
在运行配置文件中,我已经设置了
-Dfile.encoding=UTF-8
-Dsun.jnu.encoding=UTF-8 -Dfile.encoding=UTF-8 \
更新 1: 调试了一段时间后,我发现我创建的新文件的 CanonicalPath 是错误的,但我不知道为什么会这样。
- 获取路径:/reports/template/居酒屋/template3.png
- getAbsolutePath: /reports/template/居酒屋/template3.png
- getCanonicalPath: /reports/template/???/template3.png
解决方法
经过搜索,我找到了这个问题的解决方案:
-
解决方案:将
export LANG=ja_JP.UTF-8
添加到文件运行 -
说明:
canonicalPath
是文件系统认为引用它指向的文件系统对象的规范方法的路径。所以为了让系统得到正确的canonicalPath,环境必须设置正确的语言环境,如本文档:https://docs.oracle.com/cd/E23824_01/html/E26033/glset.html。在我的问题中,正确的语言环境是 ja_JP.UTF-8