How to specify char parameter for file name (not content) in Java?

We run the Java web application on a Linux server with the standard language standard "POSIX". Some of our clients upload files containing non-ascii characters in file names. We can save these non-ascii characters in Java by unicode, but they are lost (the saved file name will contain many question marks) after we actually save the downloaded file in the file system, since the default language standard for the file system does not support is the ascii character. Is there a way to specify a char set for a file name (not content) before saving the file in Java?

+4
source share
2 answers

The portable Java API lacks the concept of character encoding the file system, since it will not be portable: Windows, for example. saves file names as unicode regardless of language. However, on Linux, the locale LC_CTYPE your locale determines the encoding of the file system. Therefore, by exporting LC_CTYPE=en_US.utf8 or similarly to the environment before running the Java application, your application will use this to process the file names.

Also see file.encoding has no effect, the LC_ALL environment variable does this , which talks about some of the internal components behind this conversion.

+3
source

If the files are completely under the control of your application and are not downloaded for use in another application, I would think about making my own encoding / decoding of the file names before saving them, for example. URLEncoder.encode(filename, "UTF-8") to display the username that you can use on disk, and URLDecoder.decode(encodedName, "UTF-8") vice versa.

0
source

Source: https://habr.com/ru/post/1441712/


All Articles