当使用Google Drive API时,我在这里下载MetadataFile((来处理文件:
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+居酒屋):
- 当路径=/reports/template/izakayaTemplate/template3.png时,方法工作文件和从Google Drive下载template3.png成功
- 当路径=/reports/template时/居酒屋/template3.png,该方法在
try (FileOutputStream fileOutputStream = new FileOutputStream(file))
行抛出FileNotFoundException
有人能为我解释一下这种行为吗?
注:
- 我使用SpringBoot 2.5,Java 8,Drive API v3
- 我在Amazonlinux1上运行这个项目,作为DaemonTool的一项服务
在运行配置文件中,我设置了
-Dfile.conding=UTF-8
-Dsun.jnu.encoding=UTF-8-Dfile.conding=UTF-8 \
更新1:经过一段时间的调试,我发现我创建的新文件的CanonicalPath是错误的,但我不知道为什么会发生这种情况。
- getPath:/reports/template/居酒屋/模板3.png
- getAbsolutePath:/reports/template/居酒屋/模板3.png
- getCanonicalPath:/reports/template//模板3.png
经过搜索,我找到了这个问题的解决方案:
-
解决方案:将
export LANG=ja_JP.UTF-8
添加到文件运行 -
解释:
canonicalPath
是文件系统考虑引用其指向的文件系统对象的规范方式的路径。因此,为了让系统获得正确的规范路径,环境必须设置正确的语言环境,如本文档所示:https://docs.oracle.com/cd/E23824_01/html/E26033/glset.html.在我的问题中,正确的语言环境是ja_JP。UTF-8