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:
name=new String(name.getBytes("GBK"),"iso-8859-1");//The converted directory name or file name.
2. Set the linux environment variable
Example:
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!