Home > Java > javaTutorial > 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

巴扎黑
Release: 2017-07-24 15:40:03
Original
4197 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template