Problem Description:
When using Org.apache.commons.net.ftp.FTPClient to create Chinese catalogues and upload Chinese file names, the Chinese in the directory name and file name appear as "??".
Reason:
In the FTP protocol, the file name code is iso-8859-1, so the directory name or filename needs to be transcoding.
Solution:
1. Convert the Chinese directory or file name to iso-8859-1 encoded characters. Reference code:
Copy Code code as follows:
String name= "directory name or filename";
Name=new String (name.getbytes ("GBK"), "iso-8859-1"), or the converted directory name or file name.
2. Set up Linux environment variables
Copy Code code as follows:
Export Lc_all= "ZH_CN. GBK "
Export lang= "ZH_CN. GBK "
Instance:
Copy Code code 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));
Ftpclient.login (Confservice.getconfvalue (portalconfcontants.ftp_client_username), ConfService
. Getconfvalue (Portalconfcontants.ftp_client_password));
Ftpclient.setfiletype (Ftpclient.binary_file_type);
//Make directory
if (path!= null &&! "". Equals (Path.trim ())) {
string[] pathes = Path.split ("/");
for (String onepath:pathes) {
if (OnePath = null | | "". Equals (Onepath.trim ()) {
continue;
}
Onepath=new String (onepath.getbytes ("GBK"), "iso-8859-1");
if (!ftpclient.changeworkingdirectory (OnePath)) {
Ftpclient.makedirectory (OnePath);
Ftpclient.changeworkingdirectory (OnePath);
}
}
}
result = Ftpclient.storefile (new String (Filename.getbytes ("GBK"), "Iso-8859-1"), new FileInputStream (file);
catch (Exception e) {
E.printstacktrace ();
finally {
Ftpclient.logout ();
}
return result;
}