Java ftpclient Upload Chinese directory, Chinese file name garbled problem solving method _java

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.