Home  >  Article  >  Java  >  Solution to the problem of garbled Chinese directory and file names uploaded by FTPClient in Java

Solution to the problem of garbled Chinese directory and file names uploaded by FTPClient in Java

巴扎黑
巴扎黑Original
2017-07-24 15:40:034161browse

Problem description:

When using org.apache.commons.net.ftp.FTPClient to create a Chinese directory and upload a Chinese file name, the Chinese in the directory name and file name is displayed as "??".

Reason:

In the FTP protocol, the file name encoding is iso-8859-1, so the directory name or file name needs to be transcoded.

Solution:

1. Convert the Chinese directory or file name to iso-8859-1 encoded characters. Reference code:

Copy code The code is as follows:

String name="Directory name or file name";

name=new String(name.getBytes("GBK"),"iso-8859-1");//The converted directory name or file name.


2. Set the linux environment variable

Copy code The code is as follows:

export LC_ALL= "zh_CN.GBK"
export LANG="zh_CN.GBK"


Example:

Copy code The code is as follows:


public boolean upLoadFile(File file, String path, String fileName) throws IOException {
boolean result = false;
FTPClient ftpClient = new FTPClient();
try {
ftpClient .connect(confService.getConfValue(PortalConfContants.FTP_CLIENT_HOST)); tants.FTP_CLIENT_PASSWORD));
        ftpClient.setFileType(FTPClient .BINARY_FILE_TYPE);

                                                                                                                                                       "/");

                                                                                                                                                                             }


onepath=new String(onepath.getBytes("GBK"),"iso-8859-1");                   ftpClient.makeDirectory(onepath ;
ftpClient.storeFile(
new String(fileName.getBytes("GBK") . logout();
      }
                                                                                                            logout();

The above is the detailed content of Solution to the problem of garbled Chinese directory and file names uploaded by FTPClient in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn