Search code examples
spring-bootjava-8google-drive-apijava-iogoogle-api-java-client

FileOutputStream throw FileNotFoundException when get File with Japanese in path


When using Google Drive API, I'm having this downloadMetadataFile() here to handle file:

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);
    }
}

When using above method with folder exists (izakayaTemplate + 居酒屋):

  • When path=/reports/template/izakayaTemplate/template3.png, the method working file and download template3.png successful from Google Drive
  • When path=/reports/template/居酒屋/template3.png, the method throw a FileNotFoundException at line try (FileOutputStream fileOutputStream = new FileOutputStream(file))

Can somebody please explain for me about this behavior?

Note:

  • I'm using SpringBoot 2.5, Java 8, Drive API v3
  • I'm running this project on Amazon linux 1 as a service by DaemonTool.

In the run config file, I have set

-Dfile.encoding=UTF-8
-Dsun.jnu.encoding=UTF-8 -Dfile.encoding=UTF-8 \

Update 1: After debug for a while, I found out that the CanonicalPath is wrong for the new file I create but I don't know why it happen.

  • getPath: /reports/template/居酒屋/template3.png
  • getAbsolutePath: /reports/template/居酒屋/template3.png
  • getCanonicalPath: /reports/template/???/template3.png

Solution

  • After searching, I have found the solution for this problem:

    • Solution: Add export LANG=ja_JP.UTF-8 to the file run
    • Explanation: canonicalPath is the path file system considers the canonical means to reference the file system object to which it points. So in order for the system to get the canonicalPath to be correct, the environment must have set up correct language environment like in this document: https://docs.oracle.com/cd/E23824_01/html/E26033/glset.html. In my question, the correct language environment is ja_JP.UTF-8