In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "FtpClient how to solve the Chinese display garbled code in the file name of creating Chinese directory." In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
Recently, I have been doing something related to file server. On the basis of the original disk storage, Ftp storage mode has been added. Apache FtpClient is selected as the client. Today in the test, found that the path of the latter Chinese file name is not supported, after consulting the relevant information finally found a solution.
Usage class:
org.apache.commons.net.ftp.FTPClient
Problem Description:
When creating a Chinese directory and uploading a Chinese file name, Chinese characters in the directory name and file name are displayed garbled. Solution:
Google some information on the Internet, FTP protocol inside, the provisions of the file name encoding iso-8859-1, so directory names or file names need to be transcoded.
So many people's solutions online are:
Convert Chinese directory or file names to iso-8859-1 encoded characters. Reference Code:
String name="directory name or file name";
name=new String(name.getBytes("GBK"),"iso-8859-1"); Many people change to the above operation, and find that the Chinese characters are no longer garbled after uploading, so they think that the problem has been solved. There are still people who can handle it as follows:
ftpClient.setControlEncoding("GBK");
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);conf.setServerLanguageCode("zh");
I have tried all the above processing methods and found that the uploaded files in Chinese are normal on the FTP server I built, not garbled. I thought the Chinese problem would be solved at that time, but when the colleagues in the testing department uploaded the files to the FTP server they built, the file paths in Chinese were still garbled, so the above solutions were wrong.
The reason why the above method is wrong is because it does not consider the encoding format of the ftp server. The Ftp server I built (windows2003 server) supports GBK encoding, so the above solution is OK, but the Ftp server built by my colleagues in the test department (serv-u) supports UTF-8 format, so the encoding mode on the client side is GBK at this time, and the setting in the ftp server set up is utf-8 encoding, so there will definitely be garbled code problems.
So what is the correct solution? We can follow FlashFXP, FileZilla and other ftp client connection tools to see how they are implemented. The following two pictures are FileZilla configuration information and command information when connecting.
Figure 1: FileZilla configuration information
Figure 2: FileZilla connection information
In Figure 2, we can see that it sends the OPTS UTF8 ON command to the server to enable the server to support UTF-8. So we can also send this command to the server just like FileZilla. If the server supports UTF-8 we use UTTF-8, otherwise we use GBK to process Chinese filenames.
Here is the Java code:
/** Local character encoding */
private static String LOCAL_CHARSET = "GBK";// FTP protocol specifies file name encoding as iso-8859-1private static String SERVER_CHARSET = "ISO-8859-1";private void connectFtpServer() {
if (ftpClient == null) {
ftpClient = new FTPClient();
}
if (ftpClient.isConnected()) {
return;
}
String host = getConfigValue(ADDRESS);
int port = Integer.valueOf(getConfigValue(PORT));String user = getConfigValue(USER);
String password = getConfigValue(PASSWORD);try {
ftpClient.connect(host, port);
if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {if (ftpClient.login(user, password)) {
if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) {//Enable server support for UTF-8, use UTF-8 encoding if server supports it, otherwise use local encoding (GBK).
LOCAL_CHARSET = "UTF-8";
}
ftpClient.setControlEncoding(LOCAL_CHARSET);ftpClient.enterLocalPassiveMode();//Set Passive Mode ftpClient.setFileType(getTransforModule());//Set Transfer Mode return;
} else {
throw new FileStorageException(
"Connet ftpServer error! Please check user or password");}
}
} catch (IOException e) {
disConnectServer();
throw new FileStorageException(
"Connet ftpServer error! Please check the Configuration");}
}
When uploading a file, the file name needs to be transcoded
fileName = new String(fileName.getBytes(LOCAL_CHARSET),SERVER_CHARSET);
"FtpClient in the creation of Chinese directory file name in the Chinese display garbled how to solve" the content of the introduction here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.